Thank you for your contribution! Sashiko AI review found 1 potential issue(s) 
to consider:
- [Medium] bpf: misreported attach_type and prog_type errors in link_create()
--

commit 64df6223de5ef67537b774b5db553b0530d3bf6e
Author: Leon Hwang <[email protected]>

bpf: Add syscall common attrs support for link_create

This commit adds syscall common attributes support to link_create() so that
an internal verifier log instance is created and finalized to report error
messages for invalid prog_fd, invalid attach_type, and invalid prog type.

Link: https://lore.kernel.org/bpf/[email protected]/

> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 849bc9136299d..61d100e794b45 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -5856,8 +5856,11 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
>  }
>  
>  #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.path_fd
> -static int link_create(union bpf_attr *attr, bpfptr_t uattr)
> +static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct 
> bpf_common_attr *attr_common,
> +                    bpfptr_t uattr_common, u32 size_common)
>  {
> +     struct bpf_verifier_log *log;
> +     struct bpf_log_attr attr_log;
>       struct bpf_prog *prog;
>       int ret;
>  
> @@ -5867,14 +5870,24 @@ static int link_create(union bpf_attr *attr, bpfptr_t 
> uattr)
>       if (attr->link_create.attach_type == BPF_STRUCT_OPS)
>               return bpf_struct_ops_link_create(attr);
>  
> +     log = bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, 
> size_common);
> +     if (IS_ERR(log))
> +             return PTR_ERR(log);
> +
>       prog = bpf_prog_get(attr->link_create.prog_fd);
> -     if (IS_ERR(prog))
> -             return PTR_ERR(prog);
> +     if (IS_ERR(prog)) {
> +             bpf_log(log, "Invalid prog_fd.\n");
> +             ret = PTR_ERR(prog);
> +             prog = NULL;
> +             goto out;
> +     }
>  
>       ret = bpf_prog_attach_check_attach_type(prog,
>                                               attr->link_create.attach_type);
> -     if (ret)
> +     if (ret) {
> +             bpf_log(log, "Invalid attach_type.\n");
>               goto out;
> +     }
>  
>       switch (prog->type) {
>       case BPF_PROG_TYPE_CGROUP_SKB:
> @@ -5953,12 +5966,19 @@ static int link_create(union bpf_attr *attr, bpfptr_t 
> uattr)
>                       ret = bpf_uprobe_multi_link_attach(attr, prog);
>               break;
>       default:
> +             bpf_log(log, "Invalid prog type.\n");
>               ret = -EINVAL;
>       }

[Severity: Medium]
Does calling bpf_prog_attach_check_attach_type() before checking prog->type
in link_create() cause unsupported program types and permission errors to
be misreported as "Invalid attach_type." instead of reaching the default
"Invalid prog type." branch?

In kernel/bpf/syscall.c, bpf_prog_attach_check_attach_type() handles any
prog->type not explicitly listed in its switch via:

kernel/bpf/syscall.c:bpf_prog_attach_check_attach_type() {
        ...
        case BPF_PROG_TYPE_CGROUP_SKB:
                if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
                        return -EPERM;
        ...
        default:
                ptype = attach_type_to_prog_type(attach_type);
                if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type)
                        return -EINVAL;
                return 0;
}

This leads to three cases where the logged message does not match the actual
failure:

1. When a caller passes a valid prog_fd for an unsupported program type
(such as BPF_PROG_TYPE_SOCKET_FILTER, BPF_PROG_TYPE_SCHED_ACT,
BPF_PROG_TYPE_RAW_TRACEPOINT, or BPF_PROG_TYPE_SYSCALL),
attach_type_to_prog_type() never returns that prog->type. As a result,
bpf_prog_attach_check_attach_type() returns -EINVAL, and link_create() logs
"Invalid attach_type.\n" and jumps to out, leaving the default "Invalid prog
type.\n" branch unreachable for almost all unsupported program types (except
BPF_PROG_TYPE_LIRC_MODE2 when CONFIG_NET=y, or networking types when
CONFIG_NET=n).

2. For BPF_PROG_TYPE_CGROUP_SKB, bpf_prog_attach_check_attach_type() checks
!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN) and returns -EPERM.
Because link_create() unconditionally logs "Invalid attach_type.\n" whenever
bpf_prog_attach_check_attach_type() returns non-zero, a permission failure
with a valid attach_type also logs "Invalid attach_type.\n".

3. Conversely, for BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_TRACING,
bpf_prog_attach_check_attach_type() only verifies that
attach_type_to_prog_type(attach_type) == prog->type. If a caller passes an
attach_type in the same family that does not match prog->expected_attach_type
(for example, BPF_TRACE_FEXIT for a BPF_TRACE_FENTRY program), it passes
bpf_prog_attach_check_attach_type() and fails later in link_create():

        case BPF_PROG_TYPE_LSM:
        case BPF_PROG_TYPE_TRACING:
                if (attr->link_create.attach_type != 
prog->expected_attach_type) {
                        ret = -EINVAL;
                        goto out;
                }

which jumps to out without logging "Invalid attach_type.\n".

[ ... ]

>       return ret;
>  }

[ ... ]

>
> -- 
> 2.51.2
>

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to