cn_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cn_proc.c - process events connector
  4. *
  5. * Copyright (C) Matt Helsley, IBM Corp. 2005
  6. * Based on cn_fork.c by Guillaume Thouvenin <guillaume.thouvenin@bull.net>
  7. * Original copyright notice follows:
  8. * Copyright (C) 2005 BULL SA.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/ktime.h>
  12. #include <linux/init.h>
  13. #include <linux/connector.h>
  14. #include <linux/gfp.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/atomic.h>
  17. #include <linux/pid_namespace.h>
  18. #include <linux/cn_proc.h>
  19. /*
  20. * Size of a cn_msg followed by a proc_event structure. Since the
  21. * sizeof struct cn_msg is a multiple of 4 bytes, but not 8 bytes, we
  22. * add one 4-byte word to the size here, and then start the actual
  23. * cn_msg structure 4 bytes into the stack buffer. The result is that
  24. * the immediately following proc_event structure is aligned to 8 bytes.
  25. */
  26. #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event) + 4)
  27. /* See comment above; we test our assumption about sizeof struct cn_msg here. */
  28. static inline struct cn_msg *buffer_to_cn_msg(__u8 *buffer)
  29. {
  30. BUILD_BUG_ON(sizeof(struct cn_msg) != 20);
  31. return (struct cn_msg *)(buffer + 4);
  32. }
  33. static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
  34. static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
  35. /* proc_event_counts is used as the sequence number of the netlink message */
  36. static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
  37. static inline void send_msg(struct cn_msg *msg)
  38. {
  39. preempt_disable();
  40. msg->seq = __this_cpu_inc_return(proc_event_counts) - 1;
  41. ((struct proc_event *)msg->data)->cpu = smp_processor_id();
  42. /*
  43. * Preemption remains disabled during send to ensure the messages are
  44. * ordered according to their sequence numbers.
  45. *
  46. * If cn_netlink_send() fails, the data is not sent.
  47. */
  48. cn_netlink_send(msg, 0, CN_IDX_PROC, GFP_NOWAIT);
  49. preempt_enable();
  50. }
  51. void proc_fork_connector(struct task_struct *task)
  52. {
  53. struct cn_msg *msg;
  54. struct proc_event *ev;
  55. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  56. struct task_struct *parent;
  57. if (atomic_read(&proc_event_num_listeners) < 1)
  58. return;
  59. msg = buffer_to_cn_msg(buffer);
  60. ev = (struct proc_event *)msg->data;
  61. memset(&ev->event_data, 0, sizeof(ev->event_data));
  62. ev->timestamp_ns = ktime_get_ns();
  63. ev->what = PROC_EVENT_FORK;
  64. rcu_read_lock();
  65. parent = rcu_dereference(task->real_parent);
  66. ev->event_data.fork.parent_pid = parent->pid;
  67. ev->event_data.fork.parent_tgid = parent->tgid;
  68. rcu_read_unlock();
  69. ev->event_data.fork.child_pid = task->pid;
  70. ev->event_data.fork.child_tgid = task->tgid;
  71. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  72. msg->ack = 0; /* not used */
  73. msg->len = sizeof(*ev);
  74. msg->flags = 0; /* not used */
  75. send_msg(msg);
  76. }
  77. void proc_exec_connector(struct task_struct *task)
  78. {
  79. struct cn_msg *msg;
  80. struct proc_event *ev;
  81. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  82. if (atomic_read(&proc_event_num_listeners) < 1)
  83. return;
  84. msg = buffer_to_cn_msg(buffer);
  85. ev = (struct proc_event *)msg->data;
  86. memset(&ev->event_data, 0, sizeof(ev->event_data));
  87. ev->timestamp_ns = ktime_get_ns();
  88. ev->what = PROC_EVENT_EXEC;
  89. ev->event_data.exec.process_pid = task->pid;
  90. ev->event_data.exec.process_tgid = task->tgid;
  91. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  92. msg->ack = 0; /* not used */
  93. msg->len = sizeof(*ev);
  94. msg->flags = 0; /* not used */
  95. send_msg(msg);
  96. }
  97. void proc_id_connector(struct task_struct *task, int which_id)
  98. {
  99. struct cn_msg *msg;
  100. struct proc_event *ev;
  101. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  102. const struct cred *cred;
  103. if (atomic_read(&proc_event_num_listeners) < 1)
  104. return;
  105. msg = buffer_to_cn_msg(buffer);
  106. ev = (struct proc_event *)msg->data;
  107. memset(&ev->event_data, 0, sizeof(ev->event_data));
  108. ev->what = which_id;
  109. ev->event_data.id.process_pid = task->pid;
  110. ev->event_data.id.process_tgid = task->tgid;
  111. rcu_read_lock();
  112. cred = __task_cred(task);
  113. if (which_id == PROC_EVENT_UID) {
  114. ev->event_data.id.r.ruid = from_kuid_munged(&init_user_ns, cred->uid);
  115. ev->event_data.id.e.euid = from_kuid_munged(&init_user_ns, cred->euid);
  116. } else if (which_id == PROC_EVENT_GID) {
  117. ev->event_data.id.r.rgid = from_kgid_munged(&init_user_ns, cred->gid);
  118. ev->event_data.id.e.egid = from_kgid_munged(&init_user_ns, cred->egid);
  119. } else {
  120. rcu_read_unlock();
  121. return;
  122. }
  123. rcu_read_unlock();
  124. ev->timestamp_ns = ktime_get_ns();
  125. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  126. msg->ack = 0; /* not used */
  127. msg->len = sizeof(*ev);
  128. msg->flags = 0; /* not used */
  129. send_msg(msg);
  130. }
  131. void proc_sid_connector(struct task_struct *task)
  132. {
  133. struct cn_msg *msg;
  134. struct proc_event *ev;
  135. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  136. if (atomic_read(&proc_event_num_listeners) < 1)
  137. return;
  138. msg = buffer_to_cn_msg(buffer);
  139. ev = (struct proc_event *)msg->data;
  140. memset(&ev->event_data, 0, sizeof(ev->event_data));
  141. ev->timestamp_ns = ktime_get_ns();
  142. ev->what = PROC_EVENT_SID;
  143. ev->event_data.sid.process_pid = task->pid;
  144. ev->event_data.sid.process_tgid = task->tgid;
  145. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  146. msg->ack = 0; /* not used */
  147. msg->len = sizeof(*ev);
  148. msg->flags = 0; /* not used */
  149. send_msg(msg);
  150. }
  151. void proc_ptrace_connector(struct task_struct *task, int ptrace_id)
  152. {
  153. struct cn_msg *msg;
  154. struct proc_event *ev;
  155. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  156. if (atomic_read(&proc_event_num_listeners) < 1)
  157. return;
  158. msg = buffer_to_cn_msg(buffer);
  159. ev = (struct proc_event *)msg->data;
  160. memset(&ev->event_data, 0, sizeof(ev->event_data));
  161. ev->timestamp_ns = ktime_get_ns();
  162. ev->what = PROC_EVENT_PTRACE;
  163. ev->event_data.ptrace.process_pid = task->pid;
  164. ev->event_data.ptrace.process_tgid = task->tgid;
  165. if (ptrace_id == PTRACE_ATTACH) {
  166. ev->event_data.ptrace.tracer_pid = current->pid;
  167. ev->event_data.ptrace.tracer_tgid = current->tgid;
  168. } else if (ptrace_id == PTRACE_DETACH) {
  169. ev->event_data.ptrace.tracer_pid = 0;
  170. ev->event_data.ptrace.tracer_tgid = 0;
  171. } else
  172. return;
  173. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  174. msg->ack = 0; /* not used */
  175. msg->len = sizeof(*ev);
  176. msg->flags = 0; /* not used */
  177. send_msg(msg);
  178. }
  179. void proc_comm_connector(struct task_struct *task)
  180. {
  181. struct cn_msg *msg;
  182. struct proc_event *ev;
  183. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  184. if (atomic_read(&proc_event_num_listeners) < 1)
  185. return;
  186. msg = buffer_to_cn_msg(buffer);
  187. ev = (struct proc_event *)msg->data;
  188. memset(&ev->event_data, 0, sizeof(ev->event_data));
  189. ev->timestamp_ns = ktime_get_ns();
  190. ev->what = PROC_EVENT_COMM;
  191. ev->event_data.comm.process_pid = task->pid;
  192. ev->event_data.comm.process_tgid = task->tgid;
  193. get_task_comm(ev->event_data.comm.comm, task);
  194. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  195. msg->ack = 0; /* not used */
  196. msg->len = sizeof(*ev);
  197. msg->flags = 0; /* not used */
  198. send_msg(msg);
  199. }
  200. void proc_coredump_connector(struct task_struct *task)
  201. {
  202. struct cn_msg *msg;
  203. struct proc_event *ev;
  204. struct task_struct *parent;
  205. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  206. if (atomic_read(&proc_event_num_listeners) < 1)
  207. return;
  208. msg = buffer_to_cn_msg(buffer);
  209. ev = (struct proc_event *)msg->data;
  210. memset(&ev->event_data, 0, sizeof(ev->event_data));
  211. ev->timestamp_ns = ktime_get_ns();
  212. ev->what = PROC_EVENT_COREDUMP;
  213. ev->event_data.coredump.process_pid = task->pid;
  214. ev->event_data.coredump.process_tgid = task->tgid;
  215. rcu_read_lock();
  216. if (pid_alive(task)) {
  217. parent = rcu_dereference(task->real_parent);
  218. ev->event_data.coredump.parent_pid = parent->pid;
  219. ev->event_data.coredump.parent_tgid = parent->tgid;
  220. }
  221. rcu_read_unlock();
  222. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  223. msg->ack = 0; /* not used */
  224. msg->len = sizeof(*ev);
  225. msg->flags = 0; /* not used */
  226. send_msg(msg);
  227. }
  228. void proc_exit_connector(struct task_struct *task)
  229. {
  230. struct cn_msg *msg;
  231. struct proc_event *ev;
  232. struct task_struct *parent;
  233. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  234. if (atomic_read(&proc_event_num_listeners) < 1)
  235. return;
  236. msg = buffer_to_cn_msg(buffer);
  237. ev = (struct proc_event *)msg->data;
  238. memset(&ev->event_data, 0, sizeof(ev->event_data));
  239. ev->timestamp_ns = ktime_get_ns();
  240. ev->what = PROC_EVENT_EXIT;
  241. ev->event_data.exit.process_pid = task->pid;
  242. ev->event_data.exit.process_tgid = task->tgid;
  243. ev->event_data.exit.exit_code = task->exit_code;
  244. ev->event_data.exit.exit_signal = task->exit_signal;
  245. rcu_read_lock();
  246. if (pid_alive(task)) {
  247. parent = rcu_dereference(task->real_parent);
  248. ev->event_data.exit.parent_pid = parent->pid;
  249. ev->event_data.exit.parent_tgid = parent->tgid;
  250. }
  251. rcu_read_unlock();
  252. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  253. msg->ack = 0; /* not used */
  254. msg->len = sizeof(*ev);
  255. msg->flags = 0; /* not used */
  256. send_msg(msg);
  257. }
  258. /*
  259. * Send an acknowledgement message to userspace
  260. *
  261. * Use 0 for success, EFOO otherwise.
  262. * Note: this is the negative of conventional kernel error
  263. * values because it's not being returned via syscall return
  264. * mechanisms.
  265. */
  266. static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
  267. {
  268. struct cn_msg *msg;
  269. struct proc_event *ev;
  270. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  271. if (atomic_read(&proc_event_num_listeners) < 1)
  272. return;
  273. msg = buffer_to_cn_msg(buffer);
  274. ev = (struct proc_event *)msg->data;
  275. memset(&ev->event_data, 0, sizeof(ev->event_data));
  276. msg->seq = rcvd_seq;
  277. ev->timestamp_ns = ktime_get_ns();
  278. ev->cpu = -1;
  279. ev->what = PROC_EVENT_NONE;
  280. ev->event_data.ack.err = err;
  281. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  282. msg->ack = rcvd_ack + 1;
  283. msg->len = sizeof(*ev);
  284. msg->flags = 0; /* not used */
  285. send_msg(msg);
  286. }
  287. /**
  288. * cn_proc_mcast_ctl
  289. * @data: message sent from userspace via the connector
  290. */
  291. static void cn_proc_mcast_ctl(struct cn_msg *msg,
  292. struct netlink_skb_parms *nsp)
  293. {
  294. enum proc_cn_mcast_op *mc_op = NULL;
  295. int err = 0;
  296. if (msg->len != sizeof(*mc_op))
  297. return;
  298. /*
  299. * Events are reported with respect to the initial pid
  300. * and user namespaces so ignore requestors from
  301. * other namespaces.
  302. */
  303. if ((current_user_ns() != &init_user_ns) ||
  304. (task_active_pid_ns(current) != &init_pid_ns))
  305. return;
  306. /* Can only change if privileged. */
  307. if (!__netlink_ns_capable(nsp, &init_user_ns, CAP_NET_ADMIN)) {
  308. err = EPERM;
  309. goto out;
  310. }
  311. mc_op = (enum proc_cn_mcast_op *)msg->data;
  312. switch (*mc_op) {
  313. case PROC_CN_MCAST_LISTEN:
  314. atomic_inc(&proc_event_num_listeners);
  315. break;
  316. case PROC_CN_MCAST_IGNORE:
  317. atomic_dec(&proc_event_num_listeners);
  318. break;
  319. default:
  320. err = EINVAL;
  321. break;
  322. }
  323. out:
  324. cn_proc_ack(err, msg->seq, msg->ack);
  325. }
  326. /*
  327. * cn_proc_init - initialization entry point
  328. *
  329. * Adds the connector callback to the connector driver.
  330. */
  331. static int __init cn_proc_init(void)
  332. {
  333. int err = cn_add_callback(&cn_proc_event_id,
  334. "cn_proc",
  335. &cn_proc_mcast_ctl);
  336. if (err) {
  337. pr_warn("cn_proc failed to register\n");
  338. return err;
  339. }
  340. return 0;
  341. }
  342. device_initcall(cn_proc_init);