cn_proc.c 11 KB

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