Debian Codegraph @

src/helper/pmon/processeventconnector.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2023 Juan Palacios <[email protected]>
3
4 #include "processeventconnector.h"
5
6 #include <linux/bpf_common.h>
7 #include <linux/cn_proc.h>
8 #include <linux/connector.h>
9 #include <linux/filter.h>
10 #include <linux/netlink.h>
11 #include <linux/version.h>
12 #include <netinet/in.h>
13 #include <string.h>
14 #include <sys/socket.h>
15 #include <sys/time.h>
16 #include <unistd.h>
17
18 #define SUBSCRIBE_MSG_SIZE \
19 NLMSG_LENGTH(sizeof(struct cn_msg) + sizeof(enum proc_cn_mcast_op))
20 #define READ_EVENT_MSG_SIZE \
21 NLMSG_LENGTH(sizeof(struct cn_msg) + sizeof(struct proc_event))
22
23 int process_event_connector_new()
24 {
25 return socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
26 }
27
28 int process_event_connector_close(int socket_fd)
29 {
30 if (socket_fd >= 0)
31 return close(socket_fd);
32
33 return 0;
34 }
35
36 int process_event_connector_set_timeout(int socket_fd, unsigned int seconds)
37 {
38 struct timeval duration;
39 duration.tv_sec = seconds;
40 duration.tv_usec = 0;
41
42 return setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, &duration,
43 sizeof(duration));
44 }
45
46 int process_event_connector_bind(int socket_fd)
47 {
48 struct sockaddr_nl sa_nl;
49 sa_nl.nl_family = AF_NETLINK;
50 sa_nl.nl_groups = CN_IDX_PROC;
51 sa_nl.nl_pid = 0; // 0 = lets the kernel to handle nl_pid
52
53 return bind(socket_fd, (struct sockaddr *)(&sa_nl), sizeof(sa_nl));
54 }
55
56 int process_event_connector_subscribe(int socket_fd, bool subscribe)
57 {
58 typedef struct nlcn_msg_t
59 {
60 struct nlmsghdr nl_hdr __attribute__((aligned(NLMSG_ALIGNTO)));
61 struct cn_msg cn_msg;
62 } nlcn_msg;
63
64 char msg_buffer[SUBSCRIBE_MSG_SIZE] = {0};
65 nlcn_msg *msg = (nlcn_msg *)msg_buffer;
66
67 msg->nl_hdr.nlmsg_len = SUBSCRIBE_MSG_SIZE;
68 msg->nl_hdr.nlmsg_pid = 0;
69 msg->nl_hdr.nlmsg_type = NLMSG_DONE;
70
71 msg->cn_msg.id.idx = CN_IDX_PROC;
72 msg->cn_msg.id.val = CN_VAL_PROC;
73 msg->cn_msg.len = sizeof(enum proc_cn_mcast_op);
74
75 enum proc_cn_mcast_op *mcast = (enum proc_cn_mcast_op *)msg->cn_msg.data;
76 *mcast = subscribe ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;
77
78 if (send(socket_fd, msg, SUBSCRIBE_MSG_SIZE, 0) < 0)
79 return -1;
80
81 return 0;
82 }
83
84 int process_event_connector_install_filter(int socket_fd)
85 {
86 struct sock_filter filter[] = {
87 // clang-format off
88
89 // check message from kernel
90 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct nlmsghdr, nlmsg_pid)),
91 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),
92 BPF_STMT(BPF_RET | BPF_K, 0x0), // drop message
93
94 // check message type NLMSG_DONE
95 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, offsetof(struct nlmsghdr, nlmsg_type)),
96 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(NLMSG_DONE), 1, 0),
97 BPF_STMT(BPF_RET | BPF_K, 0x0), // drop message
98
99 // check proc connector event CN_IDX_PROC
100 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, NLMSG_LENGTH(0) +
101 offsetof(struct cn_msg, id) +
102 offsetof(struct cb_id, idx)),
103 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(CN_IDX_PROC), 1, 0),
104 BPF_STMT(BPF_RET | BPF_K, 0x0), // drop message
105
106 // check proc connector event CN_VAL_PROC
107 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, NLMSG_LENGTH(0) +
108 offsetof(struct cn_msg, id) +
109 offsetof(struct cb_id, val)),
110 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(CN_VAL_PROC), 1, 0),
111 BPF_STMT(BPF_RET | BPF_K, 0x0), // drop message
112
113 // accept exec messages from processes
114 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, NLMSG_LENGTH(0) +
115 offsetof(struct cn_msg, data) +
116 offsetof(struct proc_event, what)),
117 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(PROC_EVENT_EXEC), 0, 6),
118
119 // processes have process_pid == process_tgid (thread group leaders)
120 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, NLMSG_LENGTH(0) +
121 offsetof(struct cn_msg, data) +
122 offsetof(struct proc_event, event_data.exec.process_pid)),
123 BPF_STMT(BPF_ST, 0),
124 BPF_STMT(BPF_LDX | BPF_W | BPF_MEM, 0),
125 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, NLMSG_LENGTH(0) +
126 offsetof(struct cn_msg, data) +
127 offsetof(struct proc_event, event_data.exec.process_tgid)),
128 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 9),
129 BPF_STMT(BPF_RET | BPF_K, 0xffffffff),
130
131 // accept exit messages from processes
132 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, NLMSG_LENGTH(0) +
133 offsetof(struct cn_msg, data) +
134 offsetof(struct proc_event, what)),
135 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(PROC_EVENT_EXIT), 0, 6),
136
137 // processes have process_pid == process_tgid
138 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, NLMSG_LENGTH(0) +
139 offsetof(struct cn_msg, data) +
140 offsetof(struct proc_event, event_data.exit.process_pid)),
141 BPF_STMT(BPF_ST, 0),
142 BPF_STMT(BPF_LDX | BPF_W | BPF_MEM, 0),
143 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, NLMSG_LENGTH(0) +
144 offsetof(struct cn_msg, data) +
145 offsetof(struct proc_event, event_data.exit.process_tgid)),
146 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 1),
147 BPF_STMT(BPF_RET | BPF_K, 0xffffffff),
148
149 // drop any other messages
150 BPF_STMT(BPF_RET | BPF_K, 0x0),
151
152 // clang-format on
153 };
154
155 struct sock_fprog fprog;
156 memset(&fprog, 0, sizeof(fprog));
157 fprog.filter = filter;
158 fprog.len = sizeof(filter) / sizeof(*filter);
159
160 return setsockopt(socket_fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog,
161 sizeof(fprog));
162 }
163
164 process_event process_event_connector_read_event(int socket_fd)
165 {
166 typedef struct nlcn_msg_t
167 {
168 struct nlmsghdr nl_hdr __attribute__((aligned(NLMSG_ALIGNTO)));
169 struct cn_msg cn_msg;
170 } nlcn_msg;
171
172 char msg_buffer[READ_EVENT_MSG_SIZE] = {0};
173 nlcn_msg *msg = (nlcn_msg *)msg_buffer;
174
175 ssize_t const rc = recv(socket_fd, msg, READ_EVENT_MSG_SIZE, 0);
176 if (rc > 0) {
177 struct proc_event *event = ((struct proc_event *)msg->cn_msg.data);
178 switch (event->what) {
179 case PROC_EVENT_EXEC:
180 return (process_event){.type = PROCESS_EVENT_EXEC,
181 .pid = event->event_data.exec.process_pid};
182
183 case PROC_EVENT_EXIT:
184 return (process_event){.type = PROCESS_EVENT_EXIT,
185 .pid = event->event_data.exit.process_pid};
186
187 default:
188 return (process_event){.type = PROCESS_EVENT_OTHER, .pid = -1};
189 }
190 }
191
192 return (process_event){.type = PROCESS_EVENT_OTHER, .pid = -1};
193 }