Commit Graph

48 Commits

Author SHA1 Message Date
Jerome Marchand 5fe883d65d bpf, lsm: Add disabled BPF LSM hook list
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 21c7063f6d08ab9afa088584939791bee0c177e5
Author: Xu Kuohai <xukuohai@huawei.com>
Date:   Fri Jul 19 19:00:51 2024 +0800

    bpf, lsm: Add disabled BPF LSM hook list

    Add a disabled hooks list for BPF LSM. progs being attached to the
    listed hooks will be rejected by the verifier.

    Suggested-by: KP Singh <kpsingh@kernel.org>
    Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
    Link: https://lore.kernel.org/r/20240719110059.797546-2-xukuohai@huaweicloud.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-13 17:36:13 +01:00
Viktor Malik 55207e3215
bpf: Add security_file_post_open() LSM hook to sleepable_lsm_hooks
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 6ddf3a9abd9fdfdd63d8c906fc1393f7950c23f4
Author: Matt Bobrowski <mattbobrowski@google.com>
Date:   Tue Jun 18 19:29:22 2024 +0000

    bpf: Add security_file_post_open() LSM hook to sleepable_lsm_hooks
    
    The new generic LSM hook security_file_post_open() was recently added
    to the LSM framework in commit 8f46ff5767b0b ("security: Introduce
    file_post_open hook"). Let's proactively add this generic LSM hook to
    the sleepable_lsm_hooks BTF ID set, because I can't see there being
    any strong reasons not to, and it's only a matter of time before
    someone else comes around and asks for it to be there.
    
    security_file_post_open() is inherently sleepable as it's purposely
    situated in the kernel that allows LSMs to directly read out the
    contents of the backing file if need be. Additionally, it's called
    directly after security_file_open(), and that LSM hook in itself
    already exists in the sleepable_lsm_hooks BTF ID set.
    
    Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20240618192923.379852-1-mattbobrowski@google.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:11 +01:00
Rado Vrbovsky 89979ba29d Merge: bpf, lsm: Add check for BPF LSM return value
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5791

JIRA: https://issues.redhat.com/browse/RHEL-63343

CVE: CVE-2024-47703
    
    commit 5d99e198be279045e6ecefe220f5c52f8ce9bfd5
    Author: Xu Kuohai <xukuohai@huawei.com>
    Date:   Fri Jul 19 19:00:52 2024 +0800
    
        bpf, lsm: Add check for BPF LSM return value
    
        A bpf prog returning a positive number attached to file_alloc_security
        hook makes kernel panic.
    
        This happens because file system can not filter out the positive number
        returned by the LSM prog using IS_ERR, and misinterprets this positive
        number as a file pointer.
    
        Given that hook file_alloc_security never returned positive number
        before the introduction of BPF LSM, and other BPF LSM hooks may
        encounter similar issues, this patch adds LSM return value check
        in verifier, to ensure no unexpected value is returned.
    
        Fixes: 520b7aa00d ("bpf: lsm: Initialize the BPF LSM hooks")
        Reported-by: Xin Liu <liuxin350@huawei.com>
        Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
        Acked-by: Eduard Zingerman <eddyz87@gmail.com>
        Link: https://lore.kernel.org/r/20240719110059.797546-3-xukuohai@huaweicloud.com
        Signed-off-by: Alexei Starovoitov <ast@kernel.org>
        Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    
Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>  
Signed-off-by: Viktor Malik <vmalik@redhat.com>

Approved-by: Luis Claudio R. Goncalves <lgoncalv@redhat.com>
Approved-by: Derek Barbosa <debarbos@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-11-22 09:15:56 +00:00
CKI Backport Bot 9ade42eda4 bpf, lsm: Add check for BPF LSM return value
JIRA: https://issues.redhat.com/browse/RHEL-63343
CVE: CVE-2024-47703

commit 5d99e198be279045e6ecefe220f5c52f8ce9bfd5
Author: Xu Kuohai <xukuohai@huawei.com>
Date:   Fri Jul 19 19:00:52 2024 +0800

    bpf, lsm: Add check for BPF LSM return value

    A bpf prog returning a positive number attached to file_alloc_security
    hook makes kernel panic.

    This happens because file system can not filter out the positive number
    returned by the LSM prog using IS_ERR, and misinterprets this positive
    number as a file pointer.

    Given that hook file_alloc_security never returned positive number
    before the introduction of BPF LSM, and other BPF LSM hooks may
    encounter similar issues, this patch adds LSM return value check
    in verifier, to ensure no unexpected value is returned.

    Fixes: 520b7aa00d ("bpf: lsm: Initialize the BPF LSM hooks")
    Reported-by: Xin Liu <liuxin350@huawei.com>
    Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20240719110059.797546-3-xukuohai@huaweicloud.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>
2024-10-21 22:38:41 +00:00
Jerome Marchand 09665ceeeb bpf: Minor clean-up to sleepable_lsm_hooks BTF set
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 1581e5118e485e82cfb5d04d636a79aaefb6f266
Author: Matt Bobrowski <mattbobrowski@google.com>
Date:   Thu Feb 1 10:43:40 2024 +0000

    bpf: Minor clean-up to sleepable_lsm_hooks BTF set

    There's already one main CONFIG_SECURITY_NETWORK ifdef block within
    the sleepable_lsm_hooks BTF set. Consolidate this duplicated ifdef
    block as there's no need for it and all things guarded by it should
    remain in one place in this specific context.

    Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/Zbt1smz43GDMbVU3@google.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:07 +02:00
Jerome Marchand 8b61bf74b0 bpf,lsm: Add BPF token LSM hooks
JIRA: https://issues.redhat.com/browse/RHEL-23649

Conflicts: Context change due to missing commits e261301c851a ("lsm:
move the remaining LSM hook comments to security/security.c") and
e1ca7129db2c ("LSM: Helpers for attribute names and filling lsm_ctx")

commit f568a3d49af9aed813a184353592efe29b0e3d16
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Jan 23 18:21:08 2024 -0800

    bpf,lsm: Add BPF token LSM hooks

    Wire up bpf_token_create and bpf_token_free LSM hooks, which allow to
    allocate LSM security blob (we add `void *security` field to struct
    bpf_token for that), but also control who can instantiate BPF token.
    This follows existing pattern for BPF map and BPF prog.

    Also add security_bpf_token_allow_cmd() and security_bpf_token_capable()
    LSM hooks that allow LSM implementation to control and negate (if
    necessary) BPF token's delegation of a specific bpf_cmd and capability,
    respectively.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Paul Moore <paul@paul-moore.com>
    Link: https://lore.kernel.org/bpf/20240124022127.2379740-12-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:03 +02:00
Jerome Marchand 51a5593860 bpf,lsm: Refactor bpf_map_alloc/bpf_map_free LSM hooks
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit a2431c7eabcf9bd5a1e7a1f7ecded40fdda4a8c5
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Jan 23 18:21:07 2024 -0800

    bpf,lsm: Refactor bpf_map_alloc/bpf_map_free LSM hooks

    Similarly to bpf_prog_alloc LSM hook, rename and extend bpf_map_alloc
    hook into bpf_map_create, taking not just struct bpf_map, but also
    bpf_attr and bpf_token, to give a fuller context to LSMs.

    Unlike bpf_prog_alloc, there is no need to move the hook around, as it
    currently is firing right before allocating BPF map ID and FD, which
    seems to be a sweet spot.

    But like bpf_prog_alloc/bpf_prog_free combo, make sure that bpf_map_free
    LSM hook is called even if bpf_map_create hook returned error, as if few
    LSMs are combined together it could be that one LSM successfully
    allocated security blob for its needs, while subsequent LSM rejected BPF
    map creation. The former LSM would still need to free up LSM blob, so we
    need to ensure security_bpf_map_free() is called regardless of the
    outcome.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Paul Moore <paul@paul-moore.com>
    Link: https://lore.kernel.org/bpf/20240124022127.2379740-11-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:03 +02:00
Jerome Marchand 8b98ff8bb2 bpf,lsm: Refactor bpf_prog_alloc/bpf_prog_free LSM hooks
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 1b67772e4e3f16cd647b229cae95fc06d120be08
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Jan 23 18:21:06 2024 -0800

    bpf,lsm: Refactor bpf_prog_alloc/bpf_prog_free LSM hooks

    Based on upstream discussion ([0]), rework existing
    bpf_prog_alloc_security LSM hook. Rename it to bpf_prog_load and instead
    of passing bpf_prog_aux, pass proper bpf_prog pointer for a full BPF
    program struct. Also, we pass bpf_attr union with all the user-provided
    arguments for BPF_PROG_LOAD command.  This will give LSMs as much
    information as we can basically provide.

    The hook is also BPF token-aware now, and optional bpf_token struct is
    passed as a third argument. bpf_prog_load LSM hook is called after
    a bunch of sanity checks were performed, bpf_prog and bpf_prog_aux were
    allocated and filled out, but right before performing full-fledged BPF
    verification step.

    bpf_prog_free LSM hook is now accepting struct bpf_prog argument, for
    consistency. SELinux code is adjusted to all new names, types, and
    signatures.

    Note, given that bpf_prog_load (previously bpf_prog_alloc) hook can be
    used by some LSMs to allocate extra security blob, but also by other
    LSMs to reject BPF program loading, we need to make sure that
    bpf_prog_free LSM hook is called after bpf_prog_load/bpf_prog_alloc one
    *even* if the hook itself returned error. If we don't do that, we run
    the risk of leaking memory. This seems to be possible today when
    combining SELinux and BPF LSM, as one example, depending on their
    relative ordering.

    Also, for BPF LSM setup, add bpf_prog_load and bpf_prog_free to
    sleepable LSM hooks list, as they are both executed in sleepable
    context. Also drop bpf_prog_load hook from untrusted, as there is no
    issue with refcount or anything else anymore, that originally forced us
    to add it to untrusted list in c0c852dd1876 ("bpf: Do not mark certain LSM
    hook arguments as trusted"). We now trigger this hook much later and it
    should not be an issue anymore.

      [0] https://lore.kernel.org/bpf/9fe88aef7deabbe87d3fc38c4aea3c69.paul@paul-moore.com/

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Paul Moore <paul@paul-moore.com>
    Link: https://lore.kernel.org/bpf/20240124022127.2379740-10-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:03 +02:00
Viktor Malik 9680ef97a0
Revert BPF token-related functionality
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit d17aff807f845cf93926c28705216639c7279110
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Dec 19 07:37:35 2023 -0800

    Revert BPF token-related functionality

    This patch includes the following revert (one  conflicting BPF FS
    patch and three token patch sets, represented by merge commits):
      - revert 0f5d5454c723 "Merge branch 'bpf-fs-mount-options-parsing-follow-ups'";
      - revert 750e785796bb "bpf: Support uid and gid when mounting bpffs";
      - revert 733763285acf "Merge branch 'bpf-token-support-in-libbpf-s-bpf-object'";
      - revert c35919dcce28 "Merge branch 'bpf-token-and-bpf-fs-based-delegation'".

    Link: https://lore.kernel.org/bpf/CAHk-=wg7JuFYwGy=GOMbRCtOL+jwSQsdUaBsRWkDVYbxipbM5A@mail.gmail.com
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 11:07:29 +02:00
Viktor Malik 4e244a0a22
bpf: add small subset of SECURITY_PATH hooks to BPF sleepable_lsm_hooks list
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit b13cddf633562b9b2c34fd63471d377019704ebe
Author: Matt Bobrowski <mattbobrowski@google.com>
Date:   Fri Dec 8 15:32:48 2023 +0000

    bpf: add small subset of SECURITY_PATH hooks to BPF sleepable_lsm_hooks list
    
    security_path_* based LSM hooks appear to be generally missing from
    the sleepable_lsm_hooks list. Initially add a small subset of them to
    the preexisting sleepable_lsm_hooks list so that sleepable BPF helpers
    like bpf_d_path() can be used from sleepable BPF LSM based programs.
    
    The security_path_* hooks added in this patch are similar to the
    security_inode_* counterparts that already exist in the
    sleepable_lsm_hooks list, and are called in roughly similar points and
    contexts. Presumably, making them OK to be also annotated as
    sleepable.
    
    Building a kernel with DEBUG_ATOMIC_SLEEP options enabled and running
    reasonable workloads stimulating activity that would be intercepted by
    such security hooks didn't show any splats.
    
    Notably, I haven't added all the security_path_* LSM hooks that are
    available as I don't need them at this point in time.
    
    Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
    Acked-by: KP Singh <kpsingh@kernel.org>
    Link: https://lore.kernel.org/r/ZXM3IHHXpNY9y82a@google.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:27 +02:00
Viktor Malik 87b08573eb
bpf,lsm: add BPF token LSM hooks
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit d734ca7b33dbf60eb15dcf7c44f3da7073356777
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Nov 30 10:52:23 2023 -0800

    bpf,lsm: add BPF token LSM hooks

    Wire up bpf_token_create and bpf_token_free LSM hooks, which allow to
    allocate LSM security blob (we add `void *security` field to struct
    bpf_token for that), but also control who can instantiate BPF token.
    This follows existing pattern for BPF map and BPF prog.

    Also add security_bpf_token_allow_cmd() and security_bpf_token_capable()
    LSM hooks that allow LSM implementation to control and negate (if
    necessary) BPF token's delegation of a specific bpf_cmd and capability,
    respectively.

    Acked-by: Paul Moore <paul@paul-moore.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20231130185229.2688956-12-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:11 +02:00
Viktor Malik 4d09c8bfa4
bpf,lsm: refactor bpf_map_alloc/bpf_map_free LSM hooks
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit 66d636d70a79c1d37e3eea67ab50969e6aaef983
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Nov 30 10:52:22 2023 -0800

    bpf,lsm: refactor bpf_map_alloc/bpf_map_free LSM hooks

    Similarly to bpf_prog_alloc LSM hook, rename and extend bpf_map_alloc
    hook into bpf_map_create, taking not just struct bpf_map, but also
    bpf_attr and bpf_token, to give a fuller context to LSMs.

    Unlike bpf_prog_alloc, there is no need to move the hook around, as it
    currently is firing right before allocating BPF map ID and FD, which
    seems to be a sweet spot.

    But like bpf_prog_alloc/bpf_prog_free combo, make sure that bpf_map_free
    LSM hook is called even if bpf_map_create hook returned error, as if few
    LSMs are combined together it could be that one LSM successfully
    allocated security blob for its needs, while subsequent LSM rejected BPF
    map creation. The former LSM would still need to free up LSM blob, so we
    need to ensure security_bpf_map_free() is called regardless of the
    outcome.

    Acked-by: Paul Moore <paul@paul-moore.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20231130185229.2688956-11-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:10 +02:00
Viktor Malik 5b685d2084
bpf,lsm: refactor bpf_prog_alloc/bpf_prog_free LSM hooks
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit c3dd6e94df7193f33f45d33303f5e85afb2a72dc
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Nov 30 10:52:21 2023 -0800

    bpf,lsm: refactor bpf_prog_alloc/bpf_prog_free LSM hooks

    Based on upstream discussion ([0]), rework existing
    bpf_prog_alloc_security LSM hook. Rename it to bpf_prog_load and instead
    of passing bpf_prog_aux, pass proper bpf_prog pointer for a full BPF
    program struct. Also, we pass bpf_attr union with all the user-provided
    arguments for BPF_PROG_LOAD command.  This will give LSMs as much
    information as we can basically provide.

    The hook is also BPF token-aware now, and optional bpf_token struct is
    passed as a third argument. bpf_prog_load LSM hook is called after
    a bunch of sanity checks were performed, bpf_prog and bpf_prog_aux were
    allocated and filled out, but right before performing full-fledged BPF
    verification step.

    bpf_prog_free LSM hook is now accepting struct bpf_prog argument, for
    consistency. SELinux code is adjusted to all new names, types, and
    signatures.

    Note, given that bpf_prog_load (previously bpf_prog_alloc) hook can be
    used by some LSMs to allocate extra security blob, but also by other
    LSMs to reject BPF program loading, we need to make sure that
    bpf_prog_free LSM hook is called after bpf_prog_load/bpf_prog_alloc one
    *even* if the hook itself returned error. If we don't do that, we run
    the risk of leaking memory. This seems to be possible today when
    combining SELinux and BPF LSM, as one example, depending on their
    relative ordering.

    Also, for BPF LSM setup, add bpf_prog_load and bpf_prog_free to
    sleepable LSM hooks list, as they are both executed in sleepable
    context. Also drop bpf_prog_load hook from untrusted, as there is no
    issue with refcount or anything else anymore, that originally forced us
    to add it to untrusted list in c0c852dd1876 ("bpf: Do not mark certain LSM
    hook arguments as trusted"). We now trigger this hook much later and it
    should not be an issue anymore.

      [0] https://lore.kernel.org/bpf/9fe88aef7deabbe87d3fc38c4aea3c69.paul@paul-moore.com/

    Acked-by: Paul Moore <paul@paul-moore.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20231130185229.2688956-10-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:10 +02:00
Jerome Marchand 06b269f7d6 bpf: Fix the kernel crash caused by bpf_setsockopt().
Bugzilla: https://bugzilla.redhat.com/2177177

commit 5416c9aea8323583e8696f0500b6142dfae80821
Author: Kui-Feng Lee <kuifeng@meta.com>
Date:   Thu Jan 26 16:17:32 2023 -0800

    bpf: Fix the kernel crash caused by bpf_setsockopt().

    The kernel crash was caused by a BPF program attached to the
    "lsm_cgroup/socket_sock_rcv_skb" hook, which performed a call to
    `bpf_setsockopt()` in order to set the TCP_NODELAY flag as an
    example. Flags like TCP_NODELAY can prompt the kernel to flush a
    socket's outgoing queue, and this hook
    "lsm_cgroup/socket_sock_rcv_skb" is frequently triggered by
    softirqs. The issue was that in certain circumstances, when
    `tcp_write_xmit()` was called to flush the queue, it would also allow
    BH (bottom-half) to run. This could lead to our program attempting to
    flush the same socket recursively, which caused a `skbuff` to be
    unlinked twice.

    `security_sock_rcv_skb()` is triggered by `tcp_filter()`. This occurs
    before the sock ownership is checked in `tcp_v4_rcv()`. Consequently,
    if a bpf program runs on `security_sock_rcv_skb()` while under softirq
    conditions, it may not possess the lock needed for `bpf_setsockopt()`,
    thus presenting an issue.

    The patch fixes this issue by ensuring that a BPF program attached to
    the "lsm_cgroup/socket_sock_rcv_skb" hook is not allowed to call
    `bpf_setsockopt()`.

    The differences from v1 are
     - changing commit log to explain holding the lock of the sock,
     - emphasizing that TCP_NODELAY is not the only flag, and
     - adding the fixes tag.

    v1: https://lore.kernel.org/bpf/20230125000244.1109228-1-kuifeng@meta.com/

    Signed-off-by: Kui-Feng Lee <kuifeng@meta.com>
    Fixes: 9113d7e48e91 ("bpf: expose bpf_{g,s}etsockopt to lsm cgroup")
    Link: https://lore.kernel.org/r/20230127001732.4162630-1-kuifeng@meta.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:21 +02:00
Jerome Marchand 48e0191dea bpf: Define sock security related BTF IDs under CONFIG_SECURITY_NETWORK
Bugzilla: https://bugzilla.redhat.com/2177177

commit cc074822465d18a2d39e0b3e2b48b6766a568db2
Author: Hou Tao <houtao1@huawei.com>
Date:   Sat Dec 17 14:21:44 2022 +0800

    bpf: Define sock security related BTF IDs under CONFIG_SECURITY_NETWORK
    
    There are warnings reported from resolve_btfids when building vmlinux
    with CONFIG_SECURITY_NETWORK disabled:
    
      WARN: resolve_btfids: unresolved symbol bpf_lsm_sk_free_security
      WARN: resolve_btfids: unresolved symbol bpf_lsm_sk_alloc_security
    
    So only define BTF IDs for these LSM hooks when CONFIG_SECURITY_NETWORK
    is enabled.
    
    Fixes: c0c852dd1876 ("bpf: Do not mark certain LSM hook arguments as trusted")
    Signed-off-by: Hou Tao <houtao1@huawei.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20221217062144.2507222-1-houtao@huaweicloud.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:19 +02:00
Jerome Marchand 4b450e77be bpf: Do not mark certain LSM hook arguments as trusted
Bugzilla: https://bugzilla.redhat.com/2177177

Conflicts: Context change due to missing commit 401e64b3a4af
("bpf-lsm: Make bpf_lsm_userns_create() sleepable")

commit c0c852dd1876dc1db4600ce951a92aadd3073b1c
Author: Yonghong Song <yhs@fb.com>
Date:   Sat Dec 3 12:49:54 2022 -0800

    bpf: Do not mark certain LSM hook arguments as trusted

    Martin mentioned that the verifier cannot assume arguments from
    LSM hook sk_alloc_security being trusted since after the hook
    is called, the sk ref_count is set to 1. This will overwrite
    the ref_count changed by the bpf program and may cause ref_count
    underflow later on.

    I then further checked some other hooks. For example,
    for bpf_lsm_file_alloc() hook in fs/file_table.c,

            f->f_cred = get_cred(cred);
            error = security_file_alloc(f);
            if (unlikely(error)) {
                    file_free_rcu(&f->f_rcuhead);
                    return ERR_PTR(error);
            }

            atomic_long_set(&f->f_count, 1);

    The input parameter 'f' to security_file_alloc() cannot be trusted
    as well.

    Specifically, I investiaged bpf_map/bpf_prog/file/sk/task alloc/free
    lsm hooks. Except bpf_map_alloc and task_alloc, arguments for all other
    hooks should not be considered as trusted. This may not be a complete
    list, but it covers common usage for sk and task.

    Fixes: 3f00c5239344 ("bpf: Allow trusted pointers to be passed to KF_TRUSTED_ARGS kfuncs")
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20221203204954.2043348-1-yhs@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:14 +02:00
Jerome Marchand 868564cc57 bpf: Introduce might_sleep field in bpf_func_proto
Bugzilla: https://bugzilla.redhat.com/2177177

commit 01685c5bddaa6df3d662c8afed5e5289fcc68e5a
Author: Yonghong Song <yhs@fb.com>
Date:   Wed Nov 23 21:32:11 2022 -0800

    bpf: Introduce might_sleep field in bpf_func_proto

    Introduce bpf_func_proto->might_sleep to indicate a particular helper
    might sleep. This will make later check whether a helper might be
    sleepable or not easier.

    Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20221124053211.2373553-1-yhs@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:12 +02:00
Artem Savkov 567036485d bpf: Use cgroup_{common,current}_func_proto in more hooks
Bugzilla: https://bugzilla.redhat.com/2166911

commit bed89185af0de0d417e29ca1798df50f161b0231
Author: Stanislav Fomichev <sdf@google.com>
Date:   Tue Aug 23 15:25:52 2022 -0700

    bpf: Use cgroup_{common,current}_func_proto in more hooks
    
    The following hooks are per-cgroup hooks but they are not
    using cgroup_{common,current}_func_proto, fix it:
    
    * BPF_PROG_TYPE_CGROUP_SKB (cg_skb)
    * BPF_PROG_TYPE_CGROUP_SOCK_ADDR (cg_sock_addr)
    * BPF_PROG_TYPE_CGROUP_SOCK (cg_sock)
    * BPF_PROG_TYPE_LSM+BPF_LSM_CGROUP
    
    Also:
    
    * move common func_proto's into cgroup func_proto handlers
    * make sure bpf_{g,s}et_retval are not accessible from recvmsg,
      getpeername and getsockname (return/errno is ignored in these
      places)
    * as a side effect, expose get_current_pid_tgid, get_current_comm_proto,
      get_current_ancestor_cgroup_id, get_cgroup_classid to more cgroup
      hooks
    
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/r/20220823222555.523590-3-sdf@google.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:03 +01:00
Artem Savkov fb0c3bb384 bpf: Only add BTF IDs for socket security hooks when CONFIG_SECURITY_NETWORK is on
Bugzilla: https://bugzilla.redhat.com/2137876

commit ef331a8d4c0061ea4d353cd0db1c9b33fd45f0f2
Author: Hou Tao <houtao1@huawei.com>
Date:   Thu Sep 1 14:51:26 2022 +0800

    bpf: Only add BTF IDs for socket security hooks when CONFIG_SECURITY_NETWORK is on
    
    When CONFIG_SECURITY_NETWORK is disabled, there will be build warnings
    from resolve_btfids:
    
      WARN: resolve_btfids: unresolved symbol bpf_lsm_socket_socketpair
      ......
      WARN: resolve_btfids: unresolved symbol bpf_lsm_inet_conn_established
    
    Fixing it by wrapping these BTF ID definitions by CONFIG_SECURITY_NETWORK.
    
    Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
    Fixes: 9113d7e48e91 ("bpf: expose bpf_{g,s}etsockopt to lsm cgroup")
    Signed-off-by: Hou Tao <houtao1@huawei.com>
    Link: https://lore.kernel.org/r/20220901065126.3856297-1-houtao@huaweicloud.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:48 +01:00
Artem Savkov 2bdbb3b286 bpf: fix lsm_cgroup build errors on esoteric configs
Bugzilla: https://bugzilla.redhat.com/2137876

commit 3908fcddc65d04e069b03be49b33fae90e424631
Author: Stanislav Fomichev <sdf@google.com>
Date:   Thu Jul 14 11:54:04 2022 -0700

    bpf: fix lsm_cgroup build errors on esoteric configs
    
    This particular ones is about having the following:
     CONFIG_BPF_LSM=y
     # CONFIG_CGROUP_BPF is not set
    
    Also, add __maybe_unused to the args for the !CONFIG_NET cases.
    
    Reported-by: kernel test robot <lkp@intel.com>
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20220714185404.3647772-1-sdf@google.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:40 +01:00
Artem Savkov eb29bf1c95 bpf: expose bpf_{g,s}etsockopt to lsm cgroup
Bugzilla: https://bugzilla.redhat.com/2137876

commit 9113d7e48e9128522b9f5a54dfd30dff10509a92
Author: Stanislav Fomichev <sdf@google.com>
Date:   Tue Jun 28 10:43:09 2022 -0700

    bpf: expose bpf_{g,s}etsockopt to lsm cgroup
    
    I don't see how to make it nice without introducing btf id lists
    for the hooks where these helpers are allowed. Some LSM hooks
    work on the locked sockets, some are triggering early and
    don't grab any locks, so have two lists for now:
    
    1. LSM hooks which trigger under socket lock - minority of the hooks,
       but ideal case for us, we can expose existing BTF-based helpers
    2. LSM hooks which trigger without socket lock, but they trigger
       early in the socket creation path where it should be safe to
       do setsockopt without any locks
    3. The rest are prohibited. I'm thinking that this use-case might
       be a good gateway to sleeping lsm cgroup hooks in the future.
       We can either expose lock/unlock operations (and add tracking
       to the verifier) or have another set of bpf_setsockopt
       wrapper that grab the locks and might sleep.
    
    Reviewed-by: Martin KaFai Lau <kafai@fb.com>
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/r/20220628174314.1216643-7-sdf@google.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:33 +01:00
Artem Savkov ee4f4249cd bpf: minimize number of allocated lsm slots per program
Bugzilla: https://bugzilla.redhat.com/2137876

commit c0e19f2c9a3edd38e4b1bdae98eb44555d02bc31
Author: Stanislav Fomichev <sdf@google.com>
Date:   Tue Jun 28 10:43:07 2022 -0700

    bpf: minimize number of allocated lsm slots per program
    
    Previous patch adds 1:1 mapping between all 211 LSM hooks
    and bpf_cgroup program array. Instead of reserving a slot per
    possible hook, reserve 10 slots per cgroup for lsm programs.
    Those slots are dynamically allocated on demand and reclaimed.
    
    struct cgroup_bpf {
    	struct bpf_prog_array *    effective[33];        /*     0   264 */
    	/* --- cacheline 4 boundary (256 bytes) was 8 bytes ago --- */
    	struct hlist_head          progs[33];            /*   264   264 */
    	/* --- cacheline 8 boundary (512 bytes) was 16 bytes ago --- */
    	u8                         flags[33];            /*   528    33 */
    
    	/* XXX 7 bytes hole, try to pack */
    
    	struct list_head           storages;             /*   568    16 */
    	/* --- cacheline 9 boundary (576 bytes) was 8 bytes ago --- */
    	struct bpf_prog_array *    inactive;             /*   584     8 */
    	struct percpu_ref          refcnt;               /*   592    16 */
    	struct work_struct         release_work;         /*   608    72 */
    
    	/* size: 680, cachelines: 11, members: 7 */
    	/* sum members: 673, holes: 1, sum holes: 7 */
    	/* last cacheline: 40 bytes */
    };
    
    Reviewed-by: Martin KaFai Lau <kafai@fb.com>
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/r/20220628174314.1216643-5-sdf@google.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:33 +01:00
Artem Savkov 9a33161b25 bpf: per-cgroup lsm flavor
Bugzilla: https://bugzilla.redhat.com/2137876

Conflicts: already applied 65d9ecfe0ca73 "bpf: Fix ref_obj_id for dynptr
data slices in verifier"

commit 69fd337a975c7e690dfe49d9cb4fe5ba1e6db44e
Author: Stanislav Fomichev <sdf@google.com>
Date:   Tue Jun 28 10:43:06 2022 -0700

    bpf: per-cgroup lsm flavor

    Allow attaching to lsm hooks in the cgroup context.

    Attaching to per-cgroup LSM works exactly like attaching
    to other per-cgroup hooks. New BPF_LSM_CGROUP is added
    to trigger new mode; the actual lsm hook we attach to is
    signaled via existing attach_btf_id.

    For the hooks that have 'struct socket' or 'struct sock' as its first
    argument, we use the cgroup associated with that socket. For the rest,
    we use 'current' cgroup (this is all on default hierarchy == v2 only).
    Note that for some hooks that work on 'struct sock' we still
    take the cgroup from 'current' because some of them work on the socket
    that hasn't been properly initialized yet.

    Behind the scenes, we allocate a shim program that is attached
    to the trampoline and runs cgroup effective BPF programs array.
    This shim has some rudimentary ref counting and can be shared
    between several programs attaching to the same lsm hook from
    different cgroups.

    Note that this patch bloats cgroup size because we add 211
    cgroup_bpf_attach_type(s) for simplicity sake. This will be
    addressed in the subsequent patch.

    Also note that we only add non-sleepable flavor for now. To enable
    sleepable use-cases, bpf_prog_run_array_cg has to grab trace rcu,
    shim programs have to be freed via trace rcu, cgroup_bpf.effective
    should be also trace-rcu-managed + maybe some other changes that
    I'm not aware of.

    Reviewed-by: Martin KaFai Lau <kafai@fb.com>
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/r/20220628174314.1216643-4-sdf@google.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:33 +01:00
Yauheni Kaliuta 1c3a7dd065 bpf, x86: Attach a cookie to fentry/fexit/fmod_ret/lsm.
Bugzilla: https://bugzilla.redhat.com/2120968

commit 2fcc82411e74e5e6aba336561cf56fb899bfae4e
Author: Kui-Feng Lee <kuifeng@fb.com>
Date:   Tue May 10 13:59:21 2022 -0700

    bpf, x86: Attach a cookie to fentry/fexit/fmod_ret/lsm.
    
    Pass a cookie along with BPF_LINK_CREATE requests.
    
    Add a bpf_cookie field to struct bpf_tracing_link to attach a cookie.
    The cookie of a bpf_tracing_link is available by calling
    bpf_get_attach_cookie when running the BPF program of the attached
    link.
    
    The value of a cookie will be set at bpf_tramp_run_ctx by the
    trampoline of the link.
    
    Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220510205923.3206889-4-kuifeng@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:03 +02:00
Jerome Marchand 2a11745d30 bpf-lsm: Make bpf_lsm_kernel_read_file() as sleepable
Bugzilla: https://bugzilla.redhat.com/2120966

commit df6b3039fa112e17555776213cab7f07c0a8d98d
Author: Roberto Sassu <roberto.sassu@huawei.com>
Date:   Wed Mar 2 12:14:02 2022 +0100

    bpf-lsm: Make bpf_lsm_kernel_read_file() as sleepable

    Make bpf_lsm_kernel_read_file() as sleepable, so that bpf_ima_inode_hash()
    or bpf_ima_file_hash() can be called inside the implementation of this
    hook.

    Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220302111404.193900-8-roberto.sassu@huawei.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:02 +02:00
Jerome Marchand 3416c86665 bpf-lsm: Introduce new helper bpf_ima_file_hash()
Bugzilla: https://bugzilla.redhat.com/2120966

commit 174b16946e39ebd369097e0f773536c91a8c1a4c
Author: Roberto Sassu <roberto.sassu@huawei.com>
Date:   Wed Mar 2 12:13:58 2022 +0100

    bpf-lsm: Introduce new helper bpf_ima_file_hash()

    ima_file_hash() has been modified to calculate the measurement of a file on
    demand, if it has not been already performed by IMA or the measurement is
    not fresh. For compatibility reasons, ima_inode_hash() remains unchanged.

    Keep the same approach in eBPF and introduce the new helper
    bpf_ima_file_hash() to take advantage of the modified behavior of
    ima_file_hash().

    Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220302111404.193900-4-roberto.sassu@huawei.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:02 +02:00
Ondrej Mosnacek d5f51bb9cb
bpf: Fix renaming task_getsecid_subj->current_getsecid_subj.
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2083580

commit 63ee956f69d8c181e5251c7ce58b84c1edec0f6a
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Mon Jan 24 20:20:51 2022 -0800

    bpf: Fix renaming task_getsecid_subj->current_getsecid_subj.

    The commit 6326948f940d missed renaming of task->current LSM hook in BTF_ID.
    Fix it to silence build warning:
    WARN: resolve_btfids: unresolved symbol bpf_lsm_task_getsecid_subj

    Fixes: 6326948f940d ("lsm: security_task_getsecid_subj() -> security_current_getsecid_subj()")
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
2022-05-10 16:23:33 +02:00
David S. Miller a52171ae7b Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says:

====================
pull-request: bpf-next 2021-06-17

The following pull-request contains BPF updates for your *net-next* tree.

We've added 50 non-merge commits during the last 25 day(s) which contain
a total of 148 files changed, 4779 insertions(+), 1248 deletions(-).

The main changes are:

1) BPF infrastructure to migrate TCP child sockets from a listener to another
   in the same reuseport group/map, from Kuniyuki Iwashima.

2) Add a provably sound, faster and more precise algorithm for tnum_mul() as
   noted in https://arxiv.org/abs/2105.05398, from Harishankar Vishwanathan.

3) Streamline error reporting changes in libbpf as planned out in the
   'libbpf: the road to v1.0' effort, from Andrii Nakryiko.

4) Add broadcast support to xdp_redirect_map(), from Hangbin Liu.

5) Extends bpf_map_lookup_and_delete_elem() functionality to 4 more map
   types, that is, {LRU_,PERCPU_,LRU_PERCPU_,}HASH, from Denis Salopek.

6) Support new LLVM relocations in libbpf to make them more linker friendly,
   also add a doc to describe the BPF backend relocations, from Yonghong Song.

7) Silence long standing KUBSAN complaints on register-based shifts in
   interpreter, from Daniel Borkmann and Eric Biggers.

8) Add dummy PT_REGS macros in libbpf to fail BPF program compilation when
   target arch cannot be determined, from Lorenz Bauer.

9) Extend AF_XDP to support large umems with 1M+ pages, from Magnus Karlsson.

10) Fix two minor libbpf tc BPF API issues, from Kumar Kartikeya Dwivedi.

11) Move libbpf BPF_SEQ_PRINTF/BPF_SNPRINTF macros that can be used by BPF
    programs to bpf_helpers.h header, from Florent Revest.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
2021-06-17 11:54:56 -07:00
Shuyi Cheng 712b78c697 bpf: Fix typo in kernel/bpf/bpf_lsm.c
Fix s/sleeable/sleepable/ typo in a comment.

Signed-off-by: Shuyi Cheng <chengshuyi@linux.alibaba.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/1623809076-97907-1-git-send-email-chengshuyi@linux.alibaba.com
2021-06-16 19:56:54 -07:00
Daniel Borkmann 5c9d706f61 bpf: Fix BPF_LSM kconfig symbol dependency
Similarly as 6bdacdb48e ("bpf: Fix BPF_JIT kconfig symbol dependency") we
need to detangle the hard BPF_LSM dependency on NET. This was previously
implicit by its dependency on BPF_JIT which itself was dependent on NET (but
without any actual/real hard dependency code-wise). Given the latter was
lifted, so should be the former as BPF_LSMs could well exist on net-less
systems. This therefore also fixes a randconfig build error recently reported
by Randy:

  ld: kernel/bpf/bpf_lsm.o: in function `bpf_lsm_func_proto':
  bpf_lsm.c:(.text+0x1a0): undefined reference to `bpf_sk_storage_get_proto'
  ld: bpf_lsm.c:(.text+0x1b8): undefined reference to `bpf_sk_storage_delete_proto'
  [...]

Fixes: b24abcff91 ("bpf, kconfig: Add consolidated menu entry for bpf with core options")
Reported-by: Randy Dunlap <rdunlap@infradead.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Randy Dunlap <rdunlap@infradead.org>
Tested-by: Randy Dunlap <rdunlap@infradead.org>
2021-05-25 21:16:23 +02:00
Linus Torvalds 9d31d23389 Networking changes for 5.13.
Core:
 
  - bpf:
 	- allow bpf programs calling kernel functions (initially to
 	  reuse TCP congestion control implementations)
 	- enable task local storage for tracing programs - remove the
 	  need to store per-task state in hash maps, and allow tracing
 	  programs access to task local storage previously added for
 	  BPF_LSM
 	- add bpf_for_each_map_elem() helper, allowing programs to
 	  walk all map elements in a more robust and easier to verify
 	  fashion
 	- sockmap: support UDP and cross-protocol BPF_SK_SKB_VERDICT
 	  redirection
 	- lpm: add support for batched ops in LPM trie
 	- add BTF_KIND_FLOAT support - mostly to allow use of BTF
 	  on s390 which has floats in its headers files
 	- improve BPF syscall documentation and extend the use of kdoc
 	  parsing scripts we already employ for bpf-helpers
 	- libbpf, bpftool: support static linking of BPF ELF files
 	- improve support for encapsulation of L2 packets
 
  - xdp: restructure redirect actions to avoid a runtime lookup,
 	improving performance by 4-8% in microbenchmarks
 
  - xsk: build skb by page (aka generic zerocopy xmit) - improve
 	performance of software AF_XDP path by 33% for devices
 	which don't need headers in the linear skb part (e.g. virtio)
 
  - nexthop: resilient next-hop groups - improve path stability
 	on next-hops group changes (incl. offload for mlxsw)
 
  - ipv6: segment routing: add support for IPv4 decapsulation
 
  - icmp: add support for RFC 8335 extended PROBE messages
 
  - inet: use bigger hash table for IP ID generation
 
  - tcp: deal better with delayed TX completions - make sure we don't
 	give up on fast TCP retransmissions only because driver is
 	slow in reporting that it completed transmitting the original
 
  - tcp: reorder tcp_congestion_ops for better cache locality
 
  - mptcp:
 	- add sockopt support for common TCP options
 	- add support for common TCP msg flags
 	- include multiple address ids in RM_ADDR
 	- add reset option support for resetting one subflow
 
  - udp: GRO L4 improvements - improve 'forward' / 'frag_list'
 	co-existence with UDP tunnel GRO, allowing the first to take
 	place correctly	even for encapsulated UDP traffic
 
  - micro-optimize dev_gro_receive() and flow dissection, avoid
 	retpoline overhead on VLAN and TEB GRO
 
  - use less memory for sysctls, add a new sysctl type, to allow using
 	u8 instead of "int" and "long" and shrink networking sysctls
 
  - veth: allow GRO without XDP - this allows aggregating UDP
 	packets before handing them off to routing, bridge, OvS, etc.
 
  - allow specifing ifindex when device is moved to another namespace
 
  - netfilter:
 	- nft_socket: add support for cgroupsv2
 	- nftables: add catch-all set element - special element used
 	  to define a default action in case normal lookup missed
 	- use net_generic infra in many modules to avoid allocating
 	  per-ns memory unnecessarily
 
  - xps: improve the xps handling to avoid potential out-of-bound
 	accesses and use-after-free when XPS change race with other
 	re-configuration under traffic
 
  - add a config knob to turn off per-cpu netdev refcnt to catch
 	underflows in testing
 
 Device APIs:
 
  - add WWAN subsystem to organize the WWAN interfaces better and
    hopefully start driving towards more unified and vendor-
    -independent APIs
 
  - ethtool:
 	- add interface for reading IEEE MIB stats (incl. mlx5 and
 	  bnxt support)
 	- allow network drivers to dump arbitrary SFP EEPROM data,
 	  current offset+length API was a poor fit for modern SFP
 	  which define EEPROM in terms of pages (incl. mlx5 support)
 
  - act_police, flow_offload: add support for packet-per-second
 	policing (incl. offload for nfp)
 
  - psample: add additional metadata attributes like transit delay
 	for packets sampled from switch HW (and corresponding egress
 	and policy-based sampling in the mlxsw driver)
 
  - dsa: improve support for sandwiched LAGs with bridge and DSA
 
  - netfilter:
 	- flowtable: use direct xmit in topologies with IP
 	  forwarding, bridging, vlans etc.
 	- nftables: counter hardware offload support
 
  - Bluetooth:
 	- improvements for firmware download w/ Intel devices
 	- add support for reading AOSP vendor capabilities
 	- add support for virtio transport driver
 
  - mac80211:
 	- allow concurrent monitor iface and ethernet rx decap
 	- set priority and queue mapping for injected frames
 
  - phy: add support for Clause-45 PHY Loopback
 
  - pci/iov: add sysfs MSI-X vector assignment interface
 	to distribute MSI-X resources to VFs (incl. mlx5 support)
 
 New hardware/drivers:
 
  - dsa: mv88e6xxx: add support for Marvell mv88e6393x -
 	11-port Ethernet switch with 8x 1-Gigabit Ethernet
 	and 3x 10-Gigabit interfaces.
 
  - dsa: support for legacy Broadcom tags used on BCM5325, BCM5365
 	and BCM63xx switches
 
  - Microchip KSZ8863 and KSZ8873; 3x 10/100Mbps Ethernet switches
 
  - ath11k: support for QCN9074 a 802.11ax device
 
  - Bluetooth: Broadcom BCM4330 and BMC4334
 
  - phy: Marvell 88X2222 transceiver support
 
  - mdio: add BCM6368 MDIO mux bus controller
 
  - r8152: support RTL8153 and RTL8156 (USB Ethernet) chips
 
  - mana: driver for Microsoft Azure Network Adapter (MANA)
 
  - Actions Semi Owl Ethernet MAC
 
  - can: driver for ETAS ES58X CAN/USB interfaces
 
 Pure driver changes:
 
  - add XDP support to: enetc, igc, stmmac
  - add AF_XDP support to: stmmac
 
  - virtio:
 	- page_to_skb() use build_skb when there's sufficient tailroom
 	  (21% improvement for 1000B UDP frames)
 	- support XDP even without dedicated Tx queues - share the Tx
 	  queues with the stack when necessary
 
  - mlx5:
 	- flow rules: add support for mirroring with conntrack,
 	  matching on ICMP, GTP, flex filters and more
 	- support packet sampling with flow offloads
 	- persist uplink representor netdev across eswitch mode
 	  changes
 	- allow coexistence of CQE compression and HW time-stamping
 	- add ethtool extended link error state reporting
 
  - ice, iavf: support flow filters, UDP Segmentation Offload
 
  - dpaa2-switch:
 	- move the driver out of staging
 	- add spanning tree (STP) support
 	- add rx copybreak support
 	- add tc flower hardware offload on ingress traffic
 
  - ionic:
 	- implement Rx page reuse
 	- support HW PTP time-stamping
 
  - octeon: support TC hardware offloads - flower matching on ingress
 	and egress ratelimitting.
 
  - stmmac:
 	- add RX frame steering based on VLAN priority in tc flower
 	- support frame preemption (FPE)
 	- intel: add cross time-stamping freq difference adjustment
 
  - ocelot:
 	- support forwarding of MRP frames in HW
 	- support multiple bridges
 	- support PTP Sync one-step timestamping
 
  - dsa: mv88e6xxx, dpaa2-switch: offload bridge port flags like
 	learning, flooding etc.
 
  - ipa: add IPA v4.5, v4.9 and v4.11 support (Qualcomm SDX55, SM8350,
 	SC7280 SoCs)
 
  - mt7601u: enable TDLS support
 
  - mt76:
 	- add support for 802.3 rx frames (mt7915/mt7615)
 	- mt7915 flash pre-calibration support
 	- mt7921/mt7663 runtime power management fixes
 
 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCAAdFiEE6jPA+I1ugmIBA4hXMUZtbf5SIrsFAmCKFPIACgkQMUZtbf5S
 Irtw0g/+NA8bWdHNgG4H5rya0pv2z3IieLRmSdDfKRQQXcJpklawc5MKVVaTee/Q
 5/QqgPdCsu1LAU6JXBKsKmyDDaMlQKdWuKbOqDSiAQKoMesZStTEHf9d851ZzgxA
 Cdb6O7BD3lBl/IN+oxNG+KcmD1LKquTPKGySq2mQtEdLO12ekAsranzmj4voKffd
 q9tBShpXQ7Dq77DLYfiQXVCvsizNcbbJFuxX0o9Lpb9+61ZyYAbogZSa9ypiZZwR
 I/9azRBtJg7UV1aD/cLuAfy66Qh7t63+rCxVazs5Os8jVO26P/jQdisnnOe/x+p9
 wYEmKm3GSu0V4SAPxkWW+ooKusflCeqDoMIuooKt6kbP6BRj540veGw3Ww/m5YFr
 7pLQkTSP/tSjuGQIdBE1LOP5LBO8DZeC8Kiop9V0fzAW9hFSZbEq25WW0bPj8QQO
 zA4Z7yWlslvxcfY2BdJX3wD8klaINkl/8fDWZFFsBdfFX2VeLtm7Xfduw34BJpvU
 rYT3oWr6PhtkPAKR32SUcemSfeWgIVU41eSshzRz3kez1NngBUuLlSGGSEaKbes5
 pZVt6pYFFVByyf6MTHFEoQvafZfEw04JILZpo4R5V8iTHzom0kD3Py064sBiXEw2
 B6t+OW4qgcxGblpFkK2lD4kR2s1TPUs0ckVO6sAy1x8q60KKKjY=
 =vcbA
 -----END PGP SIGNATURE-----

Merge tag 'net-next-5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next

Pull networking updates from Jakub Kicinski:
 "Core:

   - bpf:
        - allow bpf programs calling kernel functions (initially to
          reuse TCP congestion control implementations)
        - enable task local storage for tracing programs - remove the
          need to store per-task state in hash maps, and allow tracing
          programs access to task local storage previously added for
          BPF_LSM
        - add bpf_for_each_map_elem() helper, allowing programs to walk
          all map elements in a more robust and easier to verify fashion
        - sockmap: support UDP and cross-protocol BPF_SK_SKB_VERDICT
          redirection
        - lpm: add support for batched ops in LPM trie
        - add BTF_KIND_FLOAT support - mostly to allow use of BTF on
          s390 which has floats in its headers files
        - improve BPF syscall documentation and extend the use of kdoc
          parsing scripts we already employ for bpf-helpers
        - libbpf, bpftool: support static linking of BPF ELF files
        - improve support for encapsulation of L2 packets

   - xdp: restructure redirect actions to avoid a runtime lookup,
     improving performance by 4-8% in microbenchmarks

   - xsk: build skb by page (aka generic zerocopy xmit) - improve
     performance of software AF_XDP path by 33% for devices which don't
     need headers in the linear skb part (e.g. virtio)

   - nexthop: resilient next-hop groups - improve path stability on
     next-hops group changes (incl. offload for mlxsw)

   - ipv6: segment routing: add support for IPv4 decapsulation

   - icmp: add support for RFC 8335 extended PROBE messages

   - inet: use bigger hash table for IP ID generation

   - tcp: deal better with delayed TX completions - make sure we don't
     give up on fast TCP retransmissions only because driver is slow in
     reporting that it completed transmitting the original

   - tcp: reorder tcp_congestion_ops for better cache locality

   - mptcp:
        - add sockopt support for common TCP options
        - add support for common TCP msg flags
        - include multiple address ids in RM_ADDR
        - add reset option support for resetting one subflow

   - udp: GRO L4 improvements - improve 'forward' / 'frag_list'
     co-existence with UDP tunnel GRO, allowing the first to take place
     correctly even for encapsulated UDP traffic

   - micro-optimize dev_gro_receive() and flow dissection, avoid
     retpoline overhead on VLAN and TEB GRO

   - use less memory for sysctls, add a new sysctl type, to allow using
     u8 instead of "int" and "long" and shrink networking sysctls

   - veth: allow GRO without XDP - this allows aggregating UDP packets
     before handing them off to routing, bridge, OvS, etc.

   - allow specifing ifindex when device is moved to another namespace

   - netfilter:
        - nft_socket: add support for cgroupsv2
        - nftables: add catch-all set element - special element used to
          define a default action in case normal lookup missed
        - use net_generic infra in many modules to avoid allocating
          per-ns memory unnecessarily

   - xps: improve the xps handling to avoid potential out-of-bound
     accesses and use-after-free when XPS change race with other
     re-configuration under traffic

   - add a config knob to turn off per-cpu netdev refcnt to catch
     underflows in testing

  Device APIs:

   - add WWAN subsystem to organize the WWAN interfaces better and
     hopefully start driving towards more unified and vendor-
     independent APIs

   - ethtool:
        - add interface for reading IEEE MIB stats (incl. mlx5 and bnxt
          support)
        - allow network drivers to dump arbitrary SFP EEPROM data,
          current offset+length API was a poor fit for modern SFP which
          define EEPROM in terms of pages (incl. mlx5 support)

   - act_police, flow_offload: add support for packet-per-second
     policing (incl. offload for nfp)

   - psample: add additional metadata attributes like transit delay for
     packets sampled from switch HW (and corresponding egress and
     policy-based sampling in the mlxsw driver)

   - dsa: improve support for sandwiched LAGs with bridge and DSA

   - netfilter:
        - flowtable: use direct xmit in topologies with IP forwarding,
          bridging, vlans etc.
        - nftables: counter hardware offload support

   - Bluetooth:
        - improvements for firmware download w/ Intel devices
        - add support for reading AOSP vendor capabilities
        - add support for virtio transport driver

   - mac80211:
        - allow concurrent monitor iface and ethernet rx decap
        - set priority and queue mapping for injected frames

   - phy: add support for Clause-45 PHY Loopback

   - pci/iov: add sysfs MSI-X vector assignment interface to distribute
     MSI-X resources to VFs (incl. mlx5 support)

  New hardware/drivers:

   - dsa: mv88e6xxx: add support for Marvell mv88e6393x - 11-port
     Ethernet switch with 8x 1-Gigabit Ethernet and 3x 10-Gigabit
     interfaces.

   - dsa: support for legacy Broadcom tags used on BCM5325, BCM5365 and
     BCM63xx switches

   - Microchip KSZ8863 and KSZ8873; 3x 10/100Mbps Ethernet switches

   - ath11k: support for QCN9074 a 802.11ax device

   - Bluetooth: Broadcom BCM4330 and BMC4334

   - phy: Marvell 88X2222 transceiver support

   - mdio: add BCM6368 MDIO mux bus controller

   - r8152: support RTL8153 and RTL8156 (USB Ethernet) chips

   - mana: driver for Microsoft Azure Network Adapter (MANA)

   - Actions Semi Owl Ethernet MAC

   - can: driver for ETAS ES58X CAN/USB interfaces

  Pure driver changes:

   - add XDP support to: enetc, igc, stmmac

   - add AF_XDP support to: stmmac

   - virtio:
        - page_to_skb() use build_skb when there's sufficient tailroom
          (21% improvement for 1000B UDP frames)
        - support XDP even without dedicated Tx queues - share the Tx
          queues with the stack when necessary

   - mlx5:
        - flow rules: add support for mirroring with conntrack, matching
          on ICMP, GTP, flex filters and more
        - support packet sampling with flow offloads
        - persist uplink representor netdev across eswitch mode changes
        - allow coexistence of CQE compression and HW time-stamping
        - add ethtool extended link error state reporting

   - ice, iavf: support flow filters, UDP Segmentation Offload

   - dpaa2-switch:
        - move the driver out of staging
        - add spanning tree (STP) support
        - add rx copybreak support
        - add tc flower hardware offload on ingress traffic

   - ionic:
        - implement Rx page reuse
        - support HW PTP time-stamping

   - octeon: support TC hardware offloads - flower matching on ingress
     and egress ratelimitting.

   - stmmac:
        - add RX frame steering based on VLAN priority in tc flower
        - support frame preemption (FPE)
        - intel: add cross time-stamping freq difference adjustment

   - ocelot:
        - support forwarding of MRP frames in HW
        - support multiple bridges
        - support PTP Sync one-step timestamping

   - dsa: mv88e6xxx, dpaa2-switch: offload bridge port flags like
     learning, flooding etc.

   - ipa: add IPA v4.5, v4.9 and v4.11 support (Qualcomm SDX55, SM8350,
     SC7280 SoCs)

   - mt7601u: enable TDLS support

   - mt76:
        - add support for 802.3 rx frames (mt7915/mt7615)
        - mt7915 flash pre-calibration support
        - mt7921/mt7663 runtime power management fixes"

* tag 'net-next-5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next: (2451 commits)
  net: selftest: fix build issue if INET is disabled
  net: netrom: nr_in: Remove redundant assignment to ns
  net: tun: Remove redundant assignment to ret
  net: phy: marvell: add downshift support for M88E1240
  net: dsa: ksz: Make reg_mib_cnt a u8 as it never exceeds 255
  net/sched: act_ct: Remove redundant ct get and check
  icmp: standardize naming of RFC 8335 PROBE constants
  bpf, selftests: Update array map tests for per-cpu batched ops
  bpf: Add batched ops support for percpu array
  bpf: Implement formatted output helpers with bstr_printf
  seq_file: Add a seq_bprintf function
  sfc: adjust efx->xdp_tx_queue_count with the real number of initialized queues
  net:nfc:digital: Fix a double free in digital_tg_recv_dep_req
  net: fix a concurrency bug in l2tp_tunnel_register()
  net/smc: Remove redundant assignment to rc
  mpls: Remove redundant assignment to err
  llc2: Remove redundant assignment to rc
  net/tls: Remove redundant initialization of record
  rds: Remove redundant assignment to nr_sig
  dt-bindings: net: mdio-gpio: add compatible for microchip,mdio-smi0
  ...
2021-04-29 11:57:23 -07:00
Arnd Bergmann e2c69f3a5b bpf: Avoid old-style declaration warnings
gcc -Wextra wants type modifiers in the normal order:

kernel/bpf/bpf_lsm.c:70:1: error: 'static' is not at beginning of declaration [-Werror=old-style-declaration]
   70 | const static struct bpf_func_proto bpf_bprm_opts_set_proto = {
      | ^~~~~
kernel/bpf/bpf_lsm.c:91:1: error: 'static' is not at beginning of declaration [-Werror=old-style-declaration]
   91 | const static struct bpf_func_proto bpf_ima_inode_hash_proto = {
      | ^~~~~

Fixes: 3f6719c7b6 ("bpf: Add bpf_bprm_opts_set helper")
Fixes: 27672f0d28 ("bpf: Add a BPF helper for getting the IMA hash of an inode")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: KP Singh <kpsingh@kernel.org>
Link: https://lore.kernel.org/bpf/20210322215201.1097281-1-arnd@kernel.org
2021-03-24 09:32:28 -07:00
Paul Moore 4ebd7651bf lsm: separate security_task_getsecid() into subjective and objective variants
Of the three LSMs that implement the security_task_getsecid() LSM
hook, all three LSMs provide the task's objective security
credentials.  This turns out to be unfortunate as most of the hook's
callers seem to expect the task's subjective credentials, although
a small handful of callers do correctly expect the objective
credentials.

This patch is the first step towards fixing the problem: it splits
the existing security_task_getsecid() hook into two variants, one
for the subjective creds, one for the objective creds.

  void security_task_getsecid_subj(struct task_struct *p,
				   u32 *secid);
  void security_task_getsecid_obj(struct task_struct *p,
				  u32 *secid);

While this patch does fix all of the callers to use the correct
variant, in order to keep this patch focused on the callers and to
ease review, the LSMs continue to use the same implementation for
both hooks.  The net effect is that this patch should not change
the behavior of the kernel in any way, it will be up to the latter
LSM specific patches in this series to change the hook
implementations and return the correct credentials.

Acked-by: Mimi Zohar <zohar@linux.ibm.com> (IMA)
Acked-by: Casey Schaufler <casey@schaufler-ca.com>
Reviewed-by: Richard Guy Briggs <rgb@redhat.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
2021-03-22 15:23:32 -04:00
Song Liu a10787e6d5 bpf: Enable task local storage for tracing programs
To access per-task data, BPF programs usually creates a hash table with
pid as the key. This is not ideal because:
 1. The user need to estimate the proper size of the hash table, which may
    be inaccurate;
 2. Big hash tables are slow;
 3. To clean up the data properly during task terminations, the user need
    to write extra logic.

Task local storage overcomes these issues and offers a better option for
these per-task data. Task local storage is only available to BPF_LSM. Now
enable it for tracing programs.

Unlike LSM programs, tracing programs can be called in IRQ contexts.
Helpers that access task local storage are updated to use
raw_spin_lock_irqsave() instead of raw_spin_lock_bh().

Tracing programs can attach to functions on the task free path, e.g.
exit_creds(). To avoid allocating task local storage after
bpf_task_storage_free(). bpf_task_storage_get() is updated to not allocate
new storage when the task is not refcounted (task->usage == 0).

Signed-off-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: KP Singh <kpsingh@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20210225234319.336131-2-songliubraving@fb.com
2021-02-26 11:51:47 -08:00
Mikko Ylinen 78031381ae bpf: Drop disabled LSM hooks from the sleepable set
Some networking and keys LSM hooks are conditionally enabled
and when building the new sleepable BPF LSM hooks with those
LSM hooks disabled, the following build error occurs:

  BTFIDS  vmlinux
  FAILED unresolved symbol bpf_lsm_socket_socketpair

To fix the error, conditionally add the relevant networking/keys
LSM hooks to the sleepable set.

Fixes: 423f16108c ("bpf: Augment the set of sleepable LSM hooks")
Signed-off-by: Mikko Ylinen <mikko.ylinen@linux.intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: KP Singh <kpsingh@kernel.org>
Link: https://lore.kernel.org/bpf/20210125063936.89365-1-mikko.ylinen@linux.intel.com
2021-01-26 17:08:50 +01:00
KP Singh 27672f0d28 bpf: Add a BPF helper for getting the IMA hash of an inode
Provide a wrapper function to get the IMA hash of an inode. This helper
is useful in fingerprinting files (e.g executables on execution) and
using these fingerprints in detections like an executable unlinking
itself.

Since the ima_inode_hash can sleep, it's only allowed for sleepable
LSM hooks.

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20201124151210.1081188-3-kpsingh@chromium.org
2020-11-26 00:04:04 +01:00
KP Singh 3f6719c7b6 bpf: Add bpf_bprm_opts_set helper
The helper allows modification of certain bits on the linux_binprm
struct starting with the secureexec bit which can be updated using the
BPF_F_BPRM_SECUREEXEC flag.

secureexec can be set by the LSM for privilege gaining executions to set
the AT_SECURE auxv for glibc.  When set, the dynamic linker disables the
use of certain environment variables (like LD_PRELOAD).

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20201117232929.2156341-1-kpsingh@chromium.org
2020-11-18 01:36:27 +01:00
Jakub Kicinski 07cbce2e46 Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says:

====================
pull-request: bpf-next 2020-11-14

1) Add BTF generation for kernel modules and extend BTF infra in kernel
   e.g. support for split BTF loading and validation, from Andrii Nakryiko.

2) Support for pointers beyond pkt_end to recognize LLVM generated patterns
   on inlined branch conditions, from Alexei Starovoitov.

3) Implements bpf_local_storage for task_struct for BPF LSM, from KP Singh.

4) Enable FENTRY/FEXIT/RAW_TP tracing program to use the bpf_sk_storage
   infra, from Martin KaFai Lau.

5) Add XDP bulk APIs that introduce a defer/flush mechanism to optimize the
   XDP_REDIRECT path, from Lorenzo Bianconi.

6) Fix a potential (although rather theoretical) deadlock of hashtab in NMI
   context, from Song Liu.

7) Fixes for cross and out-of-tree build of bpftool and runqslower allowing build
   for different target archs on same source tree, from Jean-Philippe Brucker.

8) Fix error path in htab_map_alloc() triggered from syzbot, from Eric Dumazet.

9) Move functionality from test_tcpbpf_user into the test_progs framework so it
   can run in BPF CI, from Alexander Duyck.

10) Lift hashtab key_size limit to be larger than MAX_BPF_STACK, from Florian Lehner.

Note that for the fix from Song we have seen a sparse report on context
imbalance which requires changes in sparse itself for proper annotation
detection where this is currently being discussed on linux-sparse among
developers [0]. Once we have more clarification/guidance after their fix,
Song will follow-up.

  [0] https://lore.kernel.org/linux-sparse/CAHk-=wh4bx8A8dHnX612MsDO13st6uzAz1mJ1PaHHVevJx_ZCw@mail.gmail.com/T/
      https://lore.kernel.org/linux-sparse/20201109221345.uklbp3lzgq6g42zb@ltop.local/T/

* git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: (66 commits)
  net: mlx5: Add xdp tx return bulking support
  net: mvpp2: Add xdp tx return bulking support
  net: mvneta: Add xdp tx return bulking support
  net: page_pool: Add bulk support for ptr_ring
  net: xdp: Introduce bulking for xdp tx return path
  bpf: Expose bpf_d_path helper to sleepable LSM hooks
  bpf: Augment the set of sleepable LSM hooks
  bpf: selftest: Use bpf_sk_storage in FENTRY/FEXIT/RAW_TP
  bpf: Allow using bpf_sk_storage in FENTRY/FEXIT/RAW_TP
  bpf: Rename some functions in bpf_sk_storage
  bpf: Folding omem_charge() into sk_storage_charge()
  selftests/bpf: Add asm tests for pkt vs pkt_end comparison.
  selftests/bpf: Add skb_pkt_end test
  bpf: Support for pointers beyond pkt_end.
  tools/bpf: Always run the *-clean recipes
  tools/bpf: Add bootstrap/ to .gitignore
  bpf: Fix NULL dereference in bpf_task_storage
  tools/bpftool: Fix build slowdown
  tools/runqslower: Build bpftool using HOSTCC
  tools/runqslower: Enable out-of-tree build
  ...
====================

Link: https://lore.kernel.org/r/20201114020819.29584-1-daniel@iogearbox.net
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2020-11-14 09:13:41 -08:00
KP Singh 423f16108c bpf: Augment the set of sleepable LSM hooks
Update the set of sleepable hooks with the ones that do not trigger
a warning with might_fault() when exercised with the correct kernel
config options enabled, i.e.

	DEBUG_ATOMIC_SLEEP=y
	LOCKDEP=y
	PROVE_LOCKING=y

This means that a sleepable LSM eBPF program can be attached to these
LSM hooks. A new helper method bpf_lsm_is_sleepable_hook is added and
the set is maintained locally in bpf_lsm.c

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20201113005930.541956-2-kpsingh@chromium.org
2020-11-13 15:45:54 +01:00
KP Singh 6f64e47783 bpf: Update verification logic for LSM programs
The current logic checks if the name of the BTF type passed in
attach_btf_id starts with "bpf_lsm_", this is not sufficient as it also
allows attachment to non-LSM hooks like the very function that performs
this check, i.e. bpf_lsm_verify_prog.

In order to ensure that this verification logic allows attachment to
only LSM hooks, the LSM_HOOK definitions in lsm_hook_defs.h are used to
generate a BTF_ID set. Upon verification, the attach_btf_id of the
program being attached is checked for presence in this set.

Fixes: 9e4e01dfd3 ("bpf: lsm: Implement attach, detach and execution")
Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201105230651.2621917-1-kpsingh@chromium.org
2020-11-06 13:15:21 -08:00
KP Singh 4cf1bc1f10 bpf: Implement task local storage
Similar to bpf_local_storage for sockets and inodes add local storage
for task_struct.

The life-cycle of storage is managed with the life-cycle of the
task_struct.  i.e. the storage is destroyed along with the owning task
with a callback to the bpf_task_storage_free from the task_free LSM
hook.

The BPF LSM allocates an __rcu pointer to the bpf_local_storage in
the security blob which are now stackable and can co-exist with other
LSMs.

The userspace map operations can be done by using a pid fd as a key
passed to the lookup, update and delete operations.

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20201106103747.2780972-3-kpsingh@chromium.org
2020-11-06 08:08:37 -08:00
KP Singh 9e7a4d9831 bpf: Allow LSM programs to use bpf spin locks
Usage of spin locks was not allowed for tracing programs due to
insufficient preemption checks. The verifier does not currently prevent
LSM programs from using spin locks, but the helpers are not exposed
via bpf_lsm_func_proto.

Based on the discussion in [1], non-sleepable LSM programs should be
able to use bpf_spin_{lock, unlock}.

Sleepable LSM programs can be preempted which means that allowng spin
locks will need more work (disabling preemption and the verifier
ensuring that no sleepable helpers are called when a spin lock is held).

[1]: https://lore.kernel.org/bpf/20201103153132.2717326-1-kpsingh@chromium.org/T/#md601a053229287659071600d3483523f752cd2fb

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20201106103747.2780972-2-kpsingh@chromium.org
2020-11-06 08:08:37 -08:00
Martin KaFai Lau 592a349864 bpf: Change bpf_sk_storage_*() to accept ARG_PTR_TO_BTF_ID_SOCK_COMMON
This patch changes the bpf_sk_storage_*() to take
ARG_PTR_TO_BTF_ID_SOCK_COMMON such that they will work with the pointer
returned by the bpf_skc_to_*() helpers also.

A micro benchmark has been done on a "cgroup_skb/egress" bpf program
which does a bpf_sk_storage_get().  It was driven by netperf doing
a 4096 connected UDP_STREAM test with 64bytes packet.
The stats from "kernel.bpf_stats_enabled" shows no meaningful difference.

The sk_storage_get_btf_proto, sk_storage_delete_btf_proto,
btf_sk_storage_get_proto, and btf_sk_storage_delete_proto are
no longer needed, so they are removed.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Lorenz Bauer <lmb@cloudflare.com>
Link: https://lore.kernel.org/bpf/20200925000402.3856307-1-kafai@fb.com
2020-09-25 13:58:01 -07:00
KP Singh 30897832d8 bpf: Allow local storage to be used from LSM programs
Adds support for both bpf_{sk, inode}_storage_{get, delete} to be used
in LSM programs. These helpers are not used for tracing programs
(currently) as their usage is tied to the life-cycle of the object and
should only be used where the owning object won't be freed (when the
owning object is passed as an argument to the LSM hook). Thus, they
are safer to use in LSM hooks than tracing. Usage of local storage in
tracing programs will probably follow a per function based whitelist
approach.

Since the UAPI helper signature for bpf_sk_storage expect a bpf_sock,
it, leads to a compilation warning for LSM programs, it's also updated
to accept a void * pointer instead.

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200825182919.1118197-7-kpsingh@chromium.org
2020-08-25 15:00:04 -07:00
Jiri Olsa 958a3f2d2a bpf: Use tracing helpers for lsm programs
Currenty lsm uses bpf_tracing_func_proto helpers which do
not include stack trace or perf event output. It's useful
to have those for bpftrace lsm support [1].

Using tracing_prog_func_proto helpers for lsm programs.

[1] https://github.com/iovisor/bpftrace/pull/1347

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Cc: KP Singh <kpsingh@google.com>
Link: https://lore.kernel.org/bpf/20200531154255.896551-1-jolsa@kernel.org
2020-06-01 15:08:04 -07:00
KP Singh 9e4e01dfd3 bpf: lsm: Implement attach, detach and execution
JITed BPF programs are dynamically attached to the LSM hooks
using BPF trampolines. The trampoline prologue generates code to handle
conversion of the signature of the hook to the appropriate BPF context.

The allocated trampoline programs are attached to the nop functions
initialized as LSM hooks.

BPF_PROG_TYPE_LSM programs must have a GPL compatible license and
and need CAP_SYS_ADMIN (required for loading eBPF programs).

Upon attachment:

* A BPF fexit trampoline is used for LSM hooks with a void return type.
* A BPF fmod_ret trampoline is used for LSM hooks which return an
  int. The attached programs can override the return value of the
  bpf LSM hook to indicate a MAC Policy decision.

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Brendan Jackman <jackmanb@google.com>
Reviewed-by: Florent Revest <revest@google.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: James Morris <jamorris@linux.microsoft.com>
Link: https://lore.kernel.org/bpf/20200329004356.27286-5-kpsingh@chromium.org
2020-03-30 01:34:00 +02:00
KP Singh 9d3fdea789 bpf: lsm: Provide attachment points for BPF LSM programs
When CONFIG_BPF_LSM is enabled, nop functions, bpf_lsm_<hook_name>, are
generated for each LSM hook. These functions are initialized as LSM
hooks in a subsequent patch.

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Brendan Jackman <jackmanb@google.com>
Reviewed-by: Florent Revest <revest@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Yonghong Song <yhs@fb.com>
Acked-by: James Morris <jamorris@linux.microsoft.com>
Link: https://lore.kernel.org/bpf/20200329004356.27286-4-kpsingh@chromium.org
2020-03-30 01:34:00 +02:00
KP Singh fc611f47f2 bpf: Introduce BPF_PROG_TYPE_LSM
Introduce types and configs for bpf programs that can be attached to
LSM hooks. The programs can be enabled by the config option
CONFIG_BPF_LSM.

Signed-off-by: KP Singh <kpsingh@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Brendan Jackman <jackmanb@google.com>
Reviewed-by: Florent Revest <revest@google.com>
Reviewed-by: Thomas Garnier <thgarnie@google.com>
Acked-by: Yonghong Song <yhs@fb.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: James Morris <jamorris@linux.microsoft.com>
Link: https://lore.kernel.org/bpf/20200329004356.27286-2-kpsingh@chromium.org
2020-03-30 01:34:00 +02:00