1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Copyright 2020 Google LLC.
5  */
6 
7 #include "vmlinux.h"
8 #include <errno.h>
9 #include <bpf/bpf_helpers.h>
10 #include <bpf/bpf_tracing.h>
11 
12 char _license[] SEC("license") = "GPL";
13 
14 #define DUMMY_STORAGE_VALUE 0xdeadbeef
15 
16 int monitored_pid = 0;
17 int inode_storage_result = -1;
18 int sk_storage_result = -1;
19 
20 struct local_storage {
21 	struct inode *exec_inode;
22 	__u32 value;
23 };
24 
25 struct {
26 	__uint(type, BPF_MAP_TYPE_INODE_STORAGE);
27 	__uint(map_flags, BPF_F_NO_PREALLOC);
28 	__type(key, int);
29 	__type(value, struct local_storage);
30 } inode_storage_map SEC(".maps");
31 
32 struct {
33 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
34 	__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
35 	__type(key, int);
36 	__type(value, struct local_storage);
37 } sk_storage_map SEC(".maps");
38 
39 struct {
40 	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
41 	__uint(map_flags, BPF_F_NO_PREALLOC);
42 	__type(key, int);
43 	__type(value, struct local_storage);
44 } task_storage_map SEC(".maps");
45 
46 SEC("lsm/inode_unlink")
47 int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
48 {
49 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
50 	struct local_storage *storage;
51 	bool is_self_unlink;
52 
53 	if (pid != monitored_pid)
54 		return 0;
55 
56 	storage = bpf_task_storage_get(&task_storage_map,
57 				       bpf_get_current_task_btf(), 0, 0);
58 	if (storage) {
59 		/* Don't let an executable delete itself */
60 		is_self_unlink = storage->exec_inode == victim->d_inode;
61 		if (is_self_unlink)
62 			return -EPERM;
63 	}
64 
65 	return 0;
66 }
67 
68 SEC("lsm.s/inode_rename")
69 int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
70 	     struct inode *new_dir, struct dentry *new_dentry,
71 	     unsigned int flags)
72 {
73 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
74 	struct local_storage *storage;
75 	int err;
76 
77 	/* new_dentry->d_inode can be NULL when the inode is renamed to a file
78 	 * that did not exist before. The helper should be able to handle this
79 	 * NULL pointer.
80 	 */
81 	bpf_inode_storage_get(&inode_storage_map, new_dentry->d_inode, 0,
82 			      BPF_LOCAL_STORAGE_GET_F_CREATE);
83 
84 	storage = bpf_inode_storage_get(&inode_storage_map, old_dentry->d_inode,
85 					0, 0);
86 	if (!storage)
87 		return 0;
88 
89 	if (storage->value != DUMMY_STORAGE_VALUE)
90 		inode_storage_result = -1;
91 
92 	err = bpf_inode_storage_delete(&inode_storage_map, old_dentry->d_inode);
93 	if (!err)
94 		inode_storage_result = err;
95 
96 	return 0;
97 }
98 
99 SEC("lsm.s/socket_bind")
100 int BPF_PROG(socket_bind, struct socket *sock, struct sockaddr *address,
101 	     int addrlen)
102 {
103 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
104 	struct local_storage *storage;
105 	int err;
106 
107 	if (pid != monitored_pid)
108 		return 0;
109 
110 	storage = bpf_sk_storage_get(&sk_storage_map, sock->sk, 0,
111 				     BPF_LOCAL_STORAGE_GET_F_CREATE);
112 	if (!storage)
113 		return 0;
114 
115 	if (storage->value != DUMMY_STORAGE_VALUE)
116 		sk_storage_result = -1;
117 
118 	err = bpf_sk_storage_delete(&sk_storage_map, sock->sk);
119 	if (!err)
120 		sk_storage_result = err;
121 
122 	return 0;
123 }
124 
125 SEC("lsm.s/socket_post_create")
126 int BPF_PROG(socket_post_create, struct socket *sock, int family, int type,
127 	     int protocol, int kern)
128 {
129 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
130 	struct local_storage *storage;
131 
132 	if (pid != monitored_pid)
133 		return 0;
134 
135 	storage = bpf_sk_storage_get(&sk_storage_map, sock->sk, 0,
136 				     BPF_LOCAL_STORAGE_GET_F_CREATE);
137 	if (!storage)
138 		return 0;
139 
140 	storage->value = DUMMY_STORAGE_VALUE;
141 
142 	return 0;
143 }
144 
145 /* This uses the local storage to remember the inode of the binary that a
146  * process was originally executing.
147  */
148 SEC("lsm.s/bprm_committed_creds")
149 void BPF_PROG(exec, struct linux_binprm *bprm)
150 {
151 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
152 	struct local_storage *storage;
153 
154 	if (pid != monitored_pid)
155 		return;
156 
157 	storage = bpf_task_storage_get(&task_storage_map,
158 				       bpf_get_current_task_btf(), 0,
159 				       BPF_LOCAL_STORAGE_GET_F_CREATE);
160 	if (storage)
161 		storage->exec_inode = bprm->file->f_inode;
162 
163 	storage = bpf_inode_storage_get(&inode_storage_map, bprm->file->f_inode,
164 					0, BPF_LOCAL_STORAGE_GET_F_CREATE);
165 	if (!storage)
166 		return;
167 
168 	storage->value = DUMMY_STORAGE_VALUE;
169 }
170