Skip to content

Commit b6d3656

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Add common attr support for prog_load
The log buffer of common attributes would be confusing with the one in 'union bpf_attr' for BPF_PROG_LOAD. In order to clarify the usage of these two log buffers, they both can be used for logging if: * They are same, including 'log_buf', 'log_level' and 'log_size'. * One of them is missing, then another one will be used for logging. If they both have 'log_buf' but they are not same totally, return -EUSERS. Signed-off-by: Leon Hwang <[email protected]>
1 parent 8c760d6 commit b6d3656

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

kernel/bpf/syscall.c

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

6095-
static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, unsigned int size)
6095+
static int check_log_attrs(u64 log_buf, u32 log_size, u32 log_level,
6096+
struct bpf_common_attr *common_attrs)
6097+
{
6098+
if (log_buf && common_attrs->log_buf && (log_buf != common_attrs->log_buf ||
6099+
log_size != common_attrs->log_size ||
6100+
log_level != common_attrs->log_level))
6101+
return -EUSERS;
6102+
6103+
return 0;
6104+
}
6105+
6106+
static int check_prog_load_log_attrs(union bpf_attr *attr, struct bpf_common_attr *common_attrs,
6107+
bool *log_common_attrs)
6108+
{
6109+
int err;
6110+
6111+
err = check_log_attrs(attr->log_buf, attr->log_size, attr->log_level, common_attrs);
6112+
if (err)
6113+
return err;
6114+
6115+
if (!attr->log_buf && common_attrs->log_buf) {
6116+
*log_common_attrs = true;
6117+
attr->log_buf = common_attrs->log_buf;
6118+
attr->log_size = common_attrs->log_size;
6119+
attr->log_level = common_attrs->log_level;
6120+
}
6121+
6122+
return 0;
6123+
}
6124+
6125+
static int __copy_common_attr_log_true_size(bpfptr_t uattr, unsigned int size, u32 *log_true_size)
6126+
{
6127+
if (size >= offsetofend(struct bpf_common_attr, log_true_size) &&
6128+
copy_to_bpfptr_offset(uattr, offsetof(struct bpf_common_attr, log_true_size),
6129+
log_true_size, sizeof(*log_true_size)))
6130+
return -EFAULT;
6131+
6132+
return 0;
6133+
}
6134+
6135+
static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, unsigned int size,
6136+
struct bpf_common_attr *common_attrs, bpfptr_t uattr_common,
6137+
unsigned int size_common, bool log_common_attrs)
60966138
{
60976139
if (!attr->log_true_size)
60986140
return 0;
60996141

6142+
if (log_common_attrs)
6143+
return __copy_common_attr_log_true_size(uattr_common, size_common,
6144+
&attr->log_true_size);
6145+
61006146
if (size >= offsetofend(union bpf_attr, log_true_size) &&
61016147
copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size),
61026148
&attr->log_true_size, sizeof(attr->log_true_size)))
@@ -6109,6 +6155,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
61096155
bpfptr_t uattr_common, unsigned int size_common)
61106156
{
61116157
struct bpf_common_attr common_attrs;
6158+
bool log_common_attrs = false;
61126159
union bpf_attr attr;
61136160
int err, ret;
61146161

@@ -6158,9 +6205,13 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
61586205
err = map_freeze(&attr);
61596206
break;
61606207
case BPF_PROG_LOAD:
6208+
err = check_prog_load_log_attrs(&attr, &common_attrs, &log_common_attrs);
6209+
if (err)
6210+
break;
61616211
attr.log_true_size = 0;
61626212
err = bpf_prog_load(&attr, uattr);
6163-
ret = copy_prog_load_log_true_size(&attr, uattr, size);
6213+
ret = copy_prog_load_log_true_size(&attr, uattr, size, &common_attrs, uattr_common,
6214+
size_common, log_common_attrs);
61646215
err = ret ? ret : err;
61656216
break;
61666217
case BPF_OBJ_PIN:

0 commit comments

Comments
 (0)