• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Copyright 2020 Google LLC.
5  */
6 
7 #include <errno.h>
8 #include <linux/bpf.h>
9 #include <stdbool.h>
10 #include <bpf/bpf_helpers.h>
11 #include <bpf/bpf_tracing.h>
12 
13 char _license[] SEC("license") = "GPL";
14 
15 #define DUMMY_STORAGE_VALUE 0xdeadbeef
16 
17 int monitored_pid = 0;
18 int inode_storage_result = -1;
19 int sk_storage_result = -1;
20 
21 struct dummy_storage {
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 dummy_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 dummy_storage);
37 } sk_storage_map SEC(".maps");
38 
39 /* TODO Use vmlinux.h once BTF pruning for embedded types is fixed.
40  */
41 struct sock {} __attribute__((preserve_access_index));
42 struct sockaddr {} __attribute__((preserve_access_index));
43 struct socket {
44 	struct sock *sk;
45 } __attribute__((preserve_access_index));
46 
47 struct inode {} __attribute__((preserve_access_index));
48 struct dentry {
49 	struct inode *d_inode;
50 } __attribute__((preserve_access_index));
51 struct file {
52 	struct inode *f_inode;
53 } __attribute__((preserve_access_index));
54 
55 
56 SEC("lsm/inode_unlink")
BPF_PROG(unlink_hook,struct inode * dir,struct dentry * victim)57 int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
58 {
59 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
60 	struct dummy_storage *storage;
61 	int err;
62 
63 	if (pid != monitored_pid)
64 		return 0;
65 
66 	storage = bpf_inode_storage_get(&inode_storage_map, victim->d_inode, 0,
67 					BPF_LOCAL_STORAGE_GET_F_CREATE);
68 	if (!storage)
69 		return 0;
70 
71 	if (storage->value != DUMMY_STORAGE_VALUE)
72 		inode_storage_result = -1;
73 
74 	err = bpf_inode_storage_delete(&inode_storage_map, victim->d_inode);
75 	if (!err)
76 		inode_storage_result = err;
77 
78 	return 0;
79 }
80 
81 SEC("lsm/socket_bind")
BPF_PROG(socket_bind,struct socket * sock,struct sockaddr * address,int addrlen)82 int BPF_PROG(socket_bind, struct socket *sock, struct sockaddr *address,
83 	     int addrlen)
84 {
85 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
86 	struct dummy_storage *storage;
87 	int err;
88 
89 	if (pid != monitored_pid)
90 		return 0;
91 
92 	storage = bpf_sk_storage_get(&sk_storage_map, sock->sk, 0,
93 				     BPF_LOCAL_STORAGE_GET_F_CREATE);
94 	if (!storage)
95 		return 0;
96 
97 	if (storage->value != DUMMY_STORAGE_VALUE)
98 		sk_storage_result = -1;
99 
100 	err = bpf_sk_storage_delete(&sk_storage_map, sock->sk);
101 	if (!err)
102 		sk_storage_result = err;
103 
104 	return 0;
105 }
106 
107 SEC("lsm/socket_post_create")
BPF_PROG(socket_post_create,struct socket * sock,int family,int type,int protocol,int kern)108 int BPF_PROG(socket_post_create, struct socket *sock, int family, int type,
109 	     int protocol, int kern)
110 {
111 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
112 	struct dummy_storage *storage;
113 
114 	if (pid != monitored_pid)
115 		return 0;
116 
117 	storage = bpf_sk_storage_get(&sk_storage_map, sock->sk, 0,
118 				     BPF_LOCAL_STORAGE_GET_F_CREATE);
119 	if (!storage)
120 		return 0;
121 
122 	storage->value = DUMMY_STORAGE_VALUE;
123 
124 	return 0;
125 }
126 
127 SEC("lsm/file_open")
BPF_PROG(file_open,struct file * file)128 int BPF_PROG(file_open, struct file *file)
129 {
130 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
131 	struct dummy_storage *storage;
132 
133 	if (pid != monitored_pid)
134 		return 0;
135 
136 	if (!file->f_inode)
137 		return 0;
138 
139 	storage = bpf_inode_storage_get(&inode_storage_map, file->f_inode, 0,
140 					BPF_LOCAL_STORAGE_GET_F_CREATE);
141 	if (!storage)
142 		return 0;
143 
144 	storage->value = DUMMY_STORAGE_VALUE;
145 	return 0;
146 }
147