Syzkaller reports the following crash:
RIP: 0010:check_return_code kernel/bpf/verifier.c:10575 [inline]
RIP: 0010:do_check kernel/bpf/verifier.c:12346 [inline]
RIP: 0010:do_check_common+0xb3d2/0xd250 kernel/bpf/verifier.c:14610
With the following reproducer:
bpf$PROG_LOAD_XDP(0x5, &(0x7f00000004c0)={0xd, 0x3, &(0x7f0000000000)=ANY=[@ANYBLOB="1800000000000019000000000000000095"], &(0x7f0000000300)='GPL\x00', 0x0, 0x0, 0x0, 0x0, 0x0, '\x00', 0x0, 0x2b, 0xffffffffffffffff, 0x8, 0x0, 0x0, 0x10, 0x0}, 0x80)
Because we don't enforce expected_attach_type for XDP programs,
we end up in hitting 'if (prog->expected_attach_type == BPF_LSM_CGROUP'
part in check_return_code and follow up with testing
`prog->aux->attach_func_proto->type`, but `prog->aux->attach_func_proto`
is NULL.
Add explicit prog_type check for the "Note, BPF_LSM_CGROUP that
attach ..." condition. Also, don't skip return code check for
LSM/STRUCT_OPS.
The above actually brings an issue with existing selftest which
tries to return EPERM from void inet_csk_clone. Fix the
test (and move called_socket_clone to make sure it's not
incremented in case of an error) and add a new one to explicitly
verify this condition.
Fixes: 69fd337a97
("bpf: per-cgroup lsm flavor")
Reported-by: syzbot+5cc0730bd4b4d2c5f152@syzkaller.appspotmail.com
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20220708175000.2603078-1-sdf@google.com
180 lines
3.8 KiB
C
180 lines
3.8 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "vmlinux.h"
|
|
#include "bpf_tracing_net.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
#ifndef AF_PACKET
|
|
#define AF_PACKET 17
|
|
#endif
|
|
|
|
#ifndef AF_UNIX
|
|
#define AF_UNIX 1
|
|
#endif
|
|
|
|
#ifndef EPERM
|
|
#define EPERM 1
|
|
#endif
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
|
|
__type(key, __u64);
|
|
__type(value, __u64);
|
|
} cgroup_storage SEC(".maps");
|
|
|
|
int called_socket_post_create;
|
|
int called_socket_post_create2;
|
|
int called_socket_bind;
|
|
int called_socket_bind2;
|
|
int called_socket_alloc;
|
|
int called_socket_clone;
|
|
|
|
static __always_inline int test_local_storage(void)
|
|
{
|
|
__u64 *val;
|
|
|
|
val = bpf_get_local_storage(&cgroup_storage, 0);
|
|
if (!val)
|
|
return 0;
|
|
*val += 1;
|
|
|
|
return 1;
|
|
}
|
|
|
|
static __always_inline int real_create(struct socket *sock, int family,
|
|
int protocol)
|
|
{
|
|
struct sock *sk;
|
|
int prio = 123;
|
|
|
|
/* Reject non-tx-only AF_PACKET. */
|
|
if (family == AF_PACKET && protocol != 0)
|
|
return 0; /* EPERM */
|
|
|
|
sk = sock->sk;
|
|
if (!sk)
|
|
return 1;
|
|
|
|
/* The rest of the sockets get default policy. */
|
|
if (bpf_setsockopt(sk, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
|
|
return 0; /* EPERM */
|
|
|
|
/* Make sure bpf_getsockopt is allowed and works. */
|
|
prio = 0;
|
|
if (bpf_getsockopt(sk, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
|
|
return 0; /* EPERM */
|
|
if (prio != 123)
|
|
return 0; /* EPERM */
|
|
|
|
/* Can access cgroup local storage. */
|
|
if (!test_local_storage())
|
|
return 0; /* EPERM */
|
|
|
|
return 1;
|
|
}
|
|
|
|
/* __cgroup_bpf_run_lsm_socket */
|
|
SEC("lsm_cgroup/socket_post_create")
|
|
int BPF_PROG(socket_post_create, struct socket *sock, int family,
|
|
int type, int protocol, int kern)
|
|
{
|
|
called_socket_post_create++;
|
|
return real_create(sock, family, protocol);
|
|
}
|
|
|
|
/* __cgroup_bpf_run_lsm_socket */
|
|
SEC("lsm_cgroup/socket_post_create")
|
|
int BPF_PROG(socket_post_create2, struct socket *sock, int family,
|
|
int type, int protocol, int kern)
|
|
{
|
|
called_socket_post_create2++;
|
|
return real_create(sock, family, protocol);
|
|
}
|
|
|
|
static __always_inline int real_bind(struct socket *sock,
|
|
struct sockaddr *address,
|
|
int addrlen)
|
|
{
|
|
struct sockaddr_ll sa = {};
|
|
|
|
if (sock->sk->__sk_common.skc_family != AF_PACKET)
|
|
return 1;
|
|
|
|
if (sock->sk->sk_kern_sock)
|
|
return 1;
|
|
|
|
bpf_probe_read_kernel(&sa, sizeof(sa), address);
|
|
if (sa.sll_protocol)
|
|
return 0; /* EPERM */
|
|
|
|
/* Can access cgroup local storage. */
|
|
if (!test_local_storage())
|
|
return 0; /* EPERM */
|
|
|
|
return 1;
|
|
}
|
|
|
|
/* __cgroup_bpf_run_lsm_socket */
|
|
SEC("lsm_cgroup/socket_bind")
|
|
int BPF_PROG(socket_bind, struct socket *sock, struct sockaddr *address,
|
|
int addrlen)
|
|
{
|
|
called_socket_bind++;
|
|
return real_bind(sock, address, addrlen);
|
|
}
|
|
|
|
/* __cgroup_bpf_run_lsm_socket */
|
|
SEC("lsm_cgroup/socket_bind")
|
|
int BPF_PROG(socket_bind2, struct socket *sock, struct sockaddr *address,
|
|
int addrlen)
|
|
{
|
|
called_socket_bind2++;
|
|
return real_bind(sock, address, addrlen);
|
|
}
|
|
|
|
/* __cgroup_bpf_run_lsm_current (via bpf_lsm_current_hooks) */
|
|
SEC("lsm_cgroup/sk_alloc_security")
|
|
int BPF_PROG(socket_alloc, struct sock *sk, int family, gfp_t priority)
|
|
{
|
|
called_socket_alloc++;
|
|
if (family == AF_UNIX)
|
|
return 0; /* EPERM */
|
|
|
|
/* Can access cgroup local storage. */
|
|
if (!test_local_storage())
|
|
return 0; /* EPERM */
|
|
|
|
return 1;
|
|
}
|
|
|
|
/* __cgroup_bpf_run_lsm_sock */
|
|
SEC("lsm_cgroup/inet_csk_clone")
|
|
int BPF_PROG(socket_clone, struct sock *newsk, const struct request_sock *req)
|
|
{
|
|
int prio = 234;
|
|
|
|
if (!newsk)
|
|
return 1;
|
|
|
|
/* Accepted request sockets get a different priority. */
|
|
if (bpf_setsockopt(newsk, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
|
|
return 1;
|
|
|
|
/* Make sure bpf_getsockopt is allowed and works. */
|
|
prio = 0;
|
|
if (bpf_getsockopt(newsk, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
|
|
return 1;
|
|
if (prio != 234)
|
|
return 1;
|
|
|
|
/* Can access cgroup local storage. */
|
|
if (!test_local_storage())
|
|
return 1;
|
|
|
|
called_socket_clone++;
|
|
|
|
return 1;
|
|
}
|