Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * Copyright 2020 Google LLC.
0005  */
0006 
0007 #include "vmlinux.h"
0008 #include <errno.h>
0009 #include <bpf/bpf_helpers.h>
0010 #include <bpf/bpf_tracing.h>
0011 
0012 char _license[] SEC("license") = "GPL";
0013 
0014 #define DUMMY_STORAGE_VALUE 0xdeadbeef
0015 
0016 int monitored_pid = 0;
0017 int inode_storage_result = -1;
0018 int sk_storage_result = -1;
0019 
0020 struct local_storage {
0021     struct inode *exec_inode;
0022     __u32 value;
0023 };
0024 
0025 struct {
0026     __uint(type, BPF_MAP_TYPE_INODE_STORAGE);
0027     __uint(map_flags, BPF_F_NO_PREALLOC);
0028     __type(key, int);
0029     __type(value, struct local_storage);
0030 } inode_storage_map SEC(".maps");
0031 
0032 struct {
0033     __uint(type, BPF_MAP_TYPE_SK_STORAGE);
0034     __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
0035     __type(key, int);
0036     __type(value, struct local_storage);
0037 } sk_storage_map SEC(".maps");
0038 
0039 struct {
0040     __uint(type, BPF_MAP_TYPE_SK_STORAGE);
0041     __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
0042     __type(key, int);
0043     __type(value, struct local_storage);
0044 } sk_storage_map2 SEC(".maps");
0045 
0046 struct {
0047     __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
0048     __uint(map_flags, BPF_F_NO_PREALLOC);
0049     __type(key, int);
0050     __type(value, struct local_storage);
0051 } task_storage_map SEC(".maps");
0052 
0053 SEC("lsm/inode_unlink")
0054 int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
0055 {
0056     __u32 pid = bpf_get_current_pid_tgid() >> 32;
0057     struct local_storage *storage;
0058     bool is_self_unlink;
0059 
0060     if (pid != monitored_pid)
0061         return 0;
0062 
0063     storage = bpf_task_storage_get(&task_storage_map,
0064                        bpf_get_current_task_btf(), 0, 0);
0065     if (storage) {
0066         /* Don't let an executable delete itself */
0067         is_self_unlink = storage->exec_inode == victim->d_inode;
0068         if (is_self_unlink)
0069             return -EPERM;
0070     }
0071 
0072     return 0;
0073 }
0074 
0075 SEC("lsm.s/inode_rename")
0076 int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
0077          struct inode *new_dir, struct dentry *new_dentry,
0078          unsigned int flags)
0079 {
0080     __u32 pid = bpf_get_current_pid_tgid() >> 32;
0081     struct local_storage *storage;
0082     int err;
0083 
0084     /* new_dentry->d_inode can be NULL when the inode is renamed to a file
0085      * that did not exist before. The helper should be able to handle this
0086      * NULL pointer.
0087      */
0088     bpf_inode_storage_get(&inode_storage_map, new_dentry->d_inode, 0,
0089                   BPF_LOCAL_STORAGE_GET_F_CREATE);
0090 
0091     storage = bpf_inode_storage_get(&inode_storage_map, old_dentry->d_inode,
0092                     0, 0);
0093     if (!storage)
0094         return 0;
0095 
0096     if (storage->value != DUMMY_STORAGE_VALUE)
0097         inode_storage_result = -1;
0098 
0099     err = bpf_inode_storage_delete(&inode_storage_map, old_dentry->d_inode);
0100     if (!err)
0101         inode_storage_result = err;
0102 
0103     return 0;
0104 }
0105 
0106 SEC("lsm.s/socket_bind")
0107 int BPF_PROG(socket_bind, struct socket *sock, struct sockaddr *address,
0108          int addrlen)
0109 {
0110     __u32 pid = bpf_get_current_pid_tgid() >> 32;
0111     struct local_storage *storage;
0112     int err;
0113 
0114     if (pid != monitored_pid)
0115         return 0;
0116 
0117     storage = bpf_sk_storage_get(&sk_storage_map, sock->sk, 0,
0118                      BPF_LOCAL_STORAGE_GET_F_CREATE);
0119     if (!storage)
0120         return 0;
0121 
0122     if (storage->value != DUMMY_STORAGE_VALUE)
0123         sk_storage_result = -1;
0124 
0125     /* This tests that we can associate multiple elements
0126      * with the local storage.
0127      */
0128     storage = bpf_sk_storage_get(&sk_storage_map2, sock->sk, 0,
0129                      BPF_LOCAL_STORAGE_GET_F_CREATE);
0130     if (!storage)
0131         return 0;
0132 
0133     err = bpf_sk_storage_delete(&sk_storage_map, sock->sk);
0134     if (err)
0135         return 0;
0136 
0137     err = bpf_sk_storage_delete(&sk_storage_map2, sock->sk);
0138     if (!err)
0139         sk_storage_result = err;
0140 
0141     return 0;
0142 }
0143 
0144 SEC("lsm.s/socket_post_create")
0145 int BPF_PROG(socket_post_create, struct socket *sock, int family, int type,
0146          int protocol, int kern)
0147 {
0148     __u32 pid = bpf_get_current_pid_tgid() >> 32;
0149     struct local_storage *storage;
0150 
0151     if (pid != monitored_pid)
0152         return 0;
0153 
0154     storage = bpf_sk_storage_get(&sk_storage_map, sock->sk, 0,
0155                      BPF_LOCAL_STORAGE_GET_F_CREATE);
0156     if (!storage)
0157         return 0;
0158 
0159     storage->value = DUMMY_STORAGE_VALUE;
0160 
0161     return 0;
0162 }
0163 
0164 /* This uses the local storage to remember the inode of the binary that a
0165  * process was originally executing.
0166  */
0167 SEC("lsm.s/bprm_committed_creds")
0168 void BPF_PROG(exec, struct linux_binprm *bprm)
0169 {
0170     __u32 pid = bpf_get_current_pid_tgid() >> 32;
0171     struct local_storage *storage;
0172 
0173     if (pid != monitored_pid)
0174         return;
0175 
0176     storage = bpf_task_storage_get(&task_storage_map,
0177                        bpf_get_current_task_btf(), 0,
0178                        BPF_LOCAL_STORAGE_GET_F_CREATE);
0179     if (storage)
0180         storage->exec_inode = bprm->file->f_inode;
0181 
0182     storage = bpf_inode_storage_get(&inode_storage_map, bprm->file->f_inode,
0183                     0, BPF_LOCAL_STORAGE_GET_F_CREATE);
0184     if (!storage)
0185         return;
0186 
0187     storage->value = DUMMY_STORAGE_VALUE;
0188 }