Skip to content

Commit 0146580

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Extend bpf syscall with common attributes support
Extend the 'bpf()' syscall to support a set of common attributes shared across all BPF commands: 1. 'log_buf': User-provided buffer for storing logs. 2. 'log_size': Size of the log buffer. 3. 'log_level': Log verbosity level. 4. 'log_true_size': The size of log reported by kernel. These common attributes are passed as the 4th argument to the 'bpf()' syscall, with the 5th argument specifying the size of this structure. To indicate the use of these common attributes from userspace, a new flag 'BPF_COMMON_ATTRS' ('1 << 16') is introduced. This flag is OR-ed into the 'cmd' field of the syscall. When 'cmd & BPF_COMMON_ATTRS' is set, the kernel will copy the common attributes from userspace into kernel space for use. Signed-off-by: Leon Hwang <[email protected]>
1 parent 5d00461 commit 0146580

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

include/linux/syscalls.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,
933933
asmlinkage long sys_getrandom(char __user *buf, size_t count,
934934
unsigned int flags);
935935
asmlinkage long sys_memfd_create(const char __user *uname_ptr, unsigned int flags);
936-
asmlinkage long sys_bpf(int cmd, union bpf_attr __user *attr, unsigned int size);
936+
asmlinkage long sys_bpf(int cmd, union bpf_attr __user *attr, unsigned int size,
937+
struct bpf_common_attr __user *attr_common, unsigned int size_common);
937938
asmlinkage long sys_execveat(int dfd, const char __user *filename,
938939
const char __user *const __user *argv,
939940
const char __user *const __user *envp, int flags);

include/uapi/linux/bpf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ enum bpf_cmd {
975975
BPF_TOKEN_CREATE,
976976
BPF_PROG_STREAM_READ_BY_FD,
977977
__MAX_BPF_CMD,
978+
BPF_COMMON_ATTRS = 1 << 16, /* Indicate carrying bpf_common_attr. */
978979
};
979980

980981
enum bpf_map_type {
@@ -1474,6 +1475,13 @@ struct bpf_stack_build_id {
14741475
};
14751476
};
14761477

1478+
struct bpf_common_attr {
1479+
__u64 log_buf;
1480+
__u32 log_size;
1481+
__u32 log_level;
1482+
__u32 log_true_size;
1483+
};
1484+
14771485
#define BPF_OBJ_NAME_LEN 16U
14781486

14791487
enum {

kernel/bpf/syscall.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,8 +6092,10 @@ static int prog_stream_read(union bpf_attr *attr)
60926092
return ret;
60936093
}
60946094

6095-
static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
6095+
static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
6096+
bpfptr_t uattr_common, unsigned int size_common)
60966097
{
6098+
struct bpf_common_attr common_attrs;
60976099
union bpf_attr attr;
60986100
int err;
60996101

@@ -6107,6 +6109,18 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
61076109
if (copy_from_bpfptr(&attr, uattr, size) != 0)
61086110
return -EFAULT;
61096111

6112+
memset(&common_attrs, 0, sizeof(common_attrs));
6113+
if (cmd & BPF_COMMON_ATTRS) {
6114+
err = bpf_check_uarg_tail_zero(uattr_common, sizeof(common_attrs), size_common);
6115+
if (err)
6116+
return err;
6117+
6118+
cmd &= ~BPF_COMMON_ATTRS;
6119+
size_common = min_t(u32, size_common, sizeof(common_attrs));
6120+
if (copy_from_bpfptr(&common_attrs, uattr_common, size_common) != 0)
6121+
return -EFAULT;
6122+
}
6123+
61106124
err = security_bpf(cmd, &attr, size, uattr.is_kernel);
61116125
if (err < 0)
61126126
return err;
@@ -6239,9 +6253,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
62396253
return err;
62406254
}
62416255

6242-
SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
6256+
SYSCALL_DEFINE5(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size,
6257+
struct bpf_common_attr __user *, uattr_common, unsigned int, size_common)
62436258
{
6244-
return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
6259+
return __sys_bpf(cmd, USER_BPFPTR(uattr), size, USER_BPFPTR(uattr_common), size_common);
62456260
}
62466261

62476262
static bool syscall_prog_is_valid_access(int off, int size,
@@ -6272,7 +6287,7 @@ BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
62726287
default:
62736288
return -EINVAL;
62746289
}
6275-
return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
6290+
return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size, KERNEL_BPFPTR(NULL), 0);
62766291
}
62776292

62786293

tools/include/uapi/linux/bpf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ enum bpf_cmd {
975975
BPF_TOKEN_CREATE,
976976
BPF_PROG_STREAM_READ_BY_FD,
977977
__MAX_BPF_CMD,
978+
BPF_COMMON_ATTRS = 1 << 16, /* Indicate carrying bpf_common_attr. */
978979
};
979980

980981
enum bpf_map_type {
@@ -1474,6 +1475,13 @@ struct bpf_stack_build_id {
14741475
};
14751476
};
14761477

1478+
struct bpf_common_attr {
1479+
__u64 log_buf;
1480+
__u32 log_size;
1481+
__u32 log_level;
1482+
__u32 log_true_size;
1483+
};
1484+
14771485
#define BPF_OBJ_NAME_LEN 16U
14781486

14791487
enum {

0 commit comments

Comments
 (0)