[PATCH 3/4] bpf: support new field for BPF_PROG_ATTACH

Dmitry V. Levin ldv at altlinux.org
Wed Mar 4 23:53:15 UTC 2020


On Sun, Feb 23, 2020 at 11:13:26AM +0100, Paul Chaignon wrote:
> * bpf_attr.h (BPF_PROG_ATTACH_struct): New field introduced in Linux
> commit v5.6-rc1~151^2~199^2~7^2~3.
> * bpf.c (BEGIN_BPF_CMD_DECODER(BPF_PROG_ATTACH)): Decode the new field.
> * NEWS: Mention this.
> * tests/bpf.c (BPF_PROG_ATTACH_checks): Update expected output.
> 
> Signed-off-by: Paul Chaignon <paul.chaignon at gmail.com>
> ---
>  NEWS        | 3 ++-
>  bpf.c       | 8 ++++++++
>  bpf_attr.h  | 3 ++-
>  tests/bpf.c | 8 +++++---
>  4 files changed, 17 insertions(+), 5 deletions(-)
> 
> diff --git a/NEWS b/NEWS
> index c54b97e0..4c7ebe1c 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -2,7 +2,8 @@ Noteworthy changes in release ?.? (????-??-??)
>  ==============================================
>  
>  * Improvements
> -  * Enhanced decoding of BPF_MAP_CREATE bpf syscall command.
> +  * Enhanced decoding of BPF_MAP_CREATE and BPF_PROG_ATTACH bpf syscall
> +    commands.
>    * Updated lists of BPF_* constants.
>  
>  * Bug fixes
> diff --git a/bpf.c b/bpf.c
> index 74d46f1b..b2335d69 100644
> --- a/bpf.c
> +++ b/bpf.c
> @@ -389,6 +389,14 @@ BEGIN_BPF_CMD_DECODER(BPF_PROG_ATTACH)
>  	PRINT_FIELD_XVAL(", ", attr, attach_type, bpf_attach_type, "BPF_???");
>  	PRINT_FIELD_FLAGS(", ", attr, attach_flags, bpf_attach_flags,
>  			  "BPF_F_???");
> +
> +	/*
> +	 * The following field was introduced by Linux commit
> +	 * v5.6-rc1~151^2~199^2~7^2~3.
> +	 */
> +	if (len <= offsetof(struct BPF_PROG_ATTACH_struct, replace_bpf_fd))
> +		break;
> +	PRINT_FIELD_FD(", ", attr, replace_bpf_fd, tcp);
>  }
>  END_BPF_CMD_DECODER(RVAL_DECODED)
>  
> diff --git a/bpf_attr.h b/bpf_attr.h
> index b569096b..e4826ec0 100644
> --- a/bpf_attr.h
> +++ b/bpf_attr.h
> @@ -151,11 +151,12 @@ struct BPF_PROG_ATTACH_struct {
>  	uint32_t attach_bpf_fd;
>  	uint32_t attach_type;
>  	uint32_t attach_flags;
> +	uint32_t replace_bpf_fd;
>  };
>  
>  # define BPF_PROG_ATTACH_struct_size \
>  	sizeof(struct BPF_PROG_ATTACH_struct)
> -# define expected_BPF_PROG_ATTACH_struct_size 16
> +# define expected_BPF_PROG_ATTACH_struct_size 20
>  
>  struct BPF_PROG_DETACH_struct {
>  	uint32_t target_fd;
> diff --git a/tests/bpf.c b/tests/bpf.c
> index 2e4894a3..d9e349af 100644
> --- a/tests/bpf.c
> +++ b/tests/bpf.c
> @@ -812,12 +812,14 @@ static const struct bpf_attr_check BPF_PROG_ATTACH_checks[] = {
>  			.target_fd = -1,
>  			.attach_bpf_fd = -2,
>  			.attach_type = 2,
> -			.attach_flags = 1
> +			.attach_flags = 1,
> +			.replace_bpf_fd = -1
>  		} },
> -		.size = offsetofend(struct BPF_PROG_ATTACH_struct, attach_flags),
> +		.size = offsetofend(struct BPF_PROG_ATTACH_struct,
> +				    replace_bpf_fd),
>  		.str = "target_fd=-1, attach_bpf_fd=-2"
>  		       ", attach_type=BPF_CGROUP_INET_SOCK_CREATE"
> -		       ", attach_flags=BPF_F_ALLOW_OVERRIDE"
> +		       ", attach_flags=BPF_F_ALLOW_OVERRIDE, replace_bpf_fd=-1"
>  	}
>  };

Eugene suggested to add a new check to BPF_PROG_ATTACH_checks instead
so that both variants of
"if (len <= offsetof(struct BPF_PROG_ATTACH_struct, replace_bpf_fd))"
condition are covered by the test.

I've pushed the following commit to ldv/next branch for testing,
please let me know if anything has to be changed.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0005-bpf-implement-decoding-of-BPF_PROG_ATTACH-replace_bp.patch
Type: text/x-patch
Size: 3606 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20200305/80fd93af/attachment.bin>


More information about the Strace-devel mailing list