Skip to content

Commit 8c760d6

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Refactor reporting log_true_size for prog_load
In the next commit, it will be able to report logs via extended common attributes, which will report 'log_true_size' via the extended common attributes meanwhile. Therefore, refactor the way of 'log_true_size' reporting in order to report 'log_true_size' via the extended common attributes easily. Signed-off-by: Leon Hwang <[email protected]>
1 parent ea127e0 commit 8c760d6

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
27382738
size_t actual_size);
27392739

27402740
/* verify correctness of eBPF program */
2741-
int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size);
2741+
int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
27422742

27432743
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
27442744
void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);

kernel/bpf/syscall.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,7 +2841,7 @@ static int bpf_prog_verify_signature(struct bpf_prog *prog, union bpf_attr *attr
28412841
/* last field in 'union bpf_attr' used by this command */
28422842
#define BPF_PROG_LOAD_LAST_FIELD keyring_id
28432843

2844-
static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
2844+
static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
28452845
{
28462846
enum bpf_prog_type type = attr->prog_type;
28472847
struct bpf_prog *prog, *dst_prog = NULL;
@@ -3059,7 +3059,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
30593059
goto free_prog_sec;
30603060

30613061
/* run eBPF verifier */
3062-
err = bpf_check(&prog, attr, uattr, uattr_size);
3062+
err = bpf_check(&prog, attr, uattr);
30633063
if (err < 0)
30643064
goto free_used_maps;
30653065

@@ -6092,12 +6092,25 @@ 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)
6096+
{
6097+
if (!attr->log_true_size)
6098+
return 0;
6099+
6100+
if (size >= offsetofend(union bpf_attr, log_true_size) &&
6101+
copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size),
6102+
&attr->log_true_size, sizeof(attr->log_true_size)))
6103+
return -EFAULT;
6104+
6105+
return 0;
6106+
}
6107+
60956108
static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
60966109
bpfptr_t uattr_common, unsigned int size_common)
60976110
{
60986111
struct bpf_common_attr common_attrs;
60996112
union bpf_attr attr;
6100-
int err;
6113+
int err, ret;
61016114

61026115
err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
61036116
if (err)
@@ -6145,7 +6158,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
61456158
err = map_freeze(&attr);
61466159
break;
61476160
case BPF_PROG_LOAD:
6148-
err = bpf_prog_load(&attr, uattr, size);
6161+
attr.log_true_size = 0;
6162+
err = bpf_prog_load(&attr, uattr);
6163+
ret = copy_prog_load_log_true_size(&attr, uattr, size);
6164+
err = ret ? ret : err;
61496165
break;
61506166
case BPF_OBJ_PIN:
61516167
err = bpf_obj_pin(&attr);

kernel/bpf/verifier.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24499,12 +24499,11 @@ static int compute_scc(struct bpf_verifier_env *env)
2449924499
return err;
2450024500
}
2450124501

24502-
int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)
24502+
int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr)
2450324503
{
2450424504
u64 start_time = ktime_get_ns();
2450524505
struct bpf_verifier_env *env;
2450624506
int i, len, ret = -EINVAL, err;
24507-
u32 log_true_size;
2450824507
bool is_priv;
2450924508

2451024509
BTF_TYPE_EMIT(enum bpf_features);
@@ -24700,17 +24699,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
2470024699
env->prog->aux->verified_insns = env->insn_processed;
2470124700

2470224701
/* preserve original error even if log finalization is successful */
24703-
err = bpf_vlog_finalize(&env->log, &log_true_size);
24702+
err = bpf_vlog_finalize(&env->log, &attr->log_true_size);
2470424703
if (err)
2470524704
ret = err;
2470624705

24707-
if (uattr_size >= offsetofend(union bpf_attr, log_true_size) &&
24708-
copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size),
24709-
&log_true_size, sizeof(log_true_size))) {
24710-
ret = -EFAULT;
24711-
goto err_release_maps;
24712-
}
24713-
2471424706
if (ret)
2471524707
goto err_release_maps;
2471624708

0 commit comments

Comments
 (0)