cn_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/cn_proc.h>
  33. /*
  34. * Size of a cn_msg followed by a proc_event structure. Since the
  35. * sizeof struct cn_msg is a multiple of 4 bytes, but not 8 bytes, we
  36. * add one 4-byte word to the size here, and then start the actual
  37. * cn_msg structure 4 bytes into the stack buffer. The result is that
  38. * the immediately following proc_event structure is aligned to 8 bytes.
  39. */
  40. #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event) + 4)
  41. /* See comment above; we test our assumption about sizeof struct cn_msg here. */
  42. static inline struct cn_msg *buffer_to_cn_msg(__u8 *buffer)
  43. {
  44. BUILD_BUG_ON(sizeof(struct cn_msg) != 20);
  45. return (struct cn_msg *)(buffer + 4);
  46. }
  47. static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
  48. static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
  49. /* proc_event_counts is used as the sequence number of the netlink message */
  50. static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
  51. static inline void get_seq(__u32 *ts, int *cpu)
  52. {
  53. preempt_disable();
  54. *ts = __this_cpu_inc_return(proc_event_counts) -1;
  55. *cpu = smp_processor_id();
  56. preempt_enable();
  57. }
  58. void proc_fork_connector(struct task_struct *task)
  59. {
  60. struct cn_msg *msg;
  61. struct proc_event *ev;
  62. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  63. struct timespec ts;
  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. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  72. ev->timestamp_ns = timespec_to_ns(&ts);
  73. ev->what = PROC_EVENT_FORK;
  74. rcu_read_lock();
  75. parent = rcu_dereference(task->real_parent);
  76. ev->event_data.fork.parent_pid = parent->pid;
  77. ev->event_data.fork.parent_tgid = parent->tgid;
  78. rcu_read_unlock();
  79. ev->event_data.fork.child_pid = task->pid;
  80. ev->event_data.fork.child_tgid = task->tgid;
  81. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  82. msg->ack = 0; /* not used */
  83. msg->len = sizeof(*ev);
  84. msg->flags = 0; /* not used */
  85. /* If cn_netlink_send() failed, the data is not sent */
  86. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  87. }
  88. void proc_exec_connector(struct task_struct *task)
  89. {
  90. struct cn_msg *msg;
  91. struct proc_event *ev;
  92. struct timespec ts;
  93. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  94. if (atomic_read(&proc_event_num_listeners) < 1)
  95. return;
  96. msg = buffer_to_cn_msg(buffer);
  97. ev = (struct proc_event*)msg->data;
  98. memset(&ev->event_data, 0, sizeof(ev->event_data));
  99. get_seq(&msg->seq, &ev->cpu);
  100. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  101. ev->timestamp_ns = timespec_to_ns(&ts);
  102. ev->what = PROC_EVENT_EXEC;
  103. ev->event_data.exec.process_pid = task->pid;
  104. ev->event_data.exec.process_tgid = task->tgid;
  105. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  106. msg->ack = 0; /* not used */
  107. msg->len = sizeof(*ev);
  108. msg->flags = 0; /* not used */
  109. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  110. }
  111. void proc_id_connector(struct task_struct *task, int which_id)
  112. {
  113. struct cn_msg *msg;
  114. struct proc_event *ev;
  115. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  116. struct timespec ts;
  117. const struct cred *cred;
  118. if (atomic_read(&proc_event_num_listeners) < 1)
  119. return;
  120. msg = buffer_to_cn_msg(buffer);
  121. ev = (struct proc_event*)msg->data;
  122. memset(&ev->event_data, 0, sizeof(ev->event_data));
  123. ev->what = which_id;
  124. ev->event_data.id.process_pid = task->pid;
  125. ev->event_data.id.process_tgid = task->tgid;
  126. rcu_read_lock();
  127. cred = __task_cred(task);
  128. if (which_id == PROC_EVENT_UID) {
  129. ev->event_data.id.r.ruid = cred->uid;
  130. ev->event_data.id.e.euid = cred->euid;
  131. } else if (which_id == PROC_EVENT_GID) {
  132. ev->event_data.id.r.rgid = cred->gid;
  133. ev->event_data.id.e.egid = cred->egid;
  134. } else {
  135. rcu_read_unlock();
  136. return;
  137. }
  138. rcu_read_unlock();
  139. get_seq(&msg->seq, &ev->cpu);
  140. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  141. ev->timestamp_ns = timespec_to_ns(&ts);
  142. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  143. msg->ack = 0; /* not used */
  144. msg->len = sizeof(*ev);
  145. msg->flags = 0; /* not used */
  146. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  147. }
  148. void proc_sid_connector(struct task_struct *task)
  149. {
  150. struct cn_msg *msg;
  151. struct proc_event *ev;
  152. struct timespec ts;
  153. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  154. if (atomic_read(&proc_event_num_listeners) < 1)
  155. return;
  156. msg = buffer_to_cn_msg(buffer);
  157. ev = (struct proc_event *)msg->data;
  158. memset(&ev->event_data, 0, sizeof(ev->event_data));
  159. get_seq(&msg->seq, &ev->cpu);
  160. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  161. ev->timestamp_ns = timespec_to_ns(&ts);
  162. ev->what = PROC_EVENT_SID;
  163. ev->event_data.sid.process_pid = task->pid;
  164. ev->event_data.sid.process_tgid = task->tgid;
  165. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  166. msg->ack = 0; /* not used */
  167. msg->len = sizeof(*ev);
  168. msg->flags = 0; /* not used */
  169. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  170. }
  171. void proc_ptrace_connector(struct task_struct *task, int ptrace_id)
  172. {
  173. struct cn_msg *msg;
  174. struct proc_event *ev;
  175. struct timespec ts;
  176. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  177. if (atomic_read(&proc_event_num_listeners) < 1)
  178. return;
  179. msg = buffer_to_cn_msg(buffer);
  180. ev = (struct proc_event *)msg->data;
  181. memset(&ev->event_data, 0, sizeof(ev->event_data));
  182. get_seq(&msg->seq, &ev->cpu);
  183. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  184. ev->timestamp_ns = timespec_to_ns(&ts);
  185. ev->what = PROC_EVENT_PTRACE;
  186. ev->event_data.ptrace.process_pid = task->pid;
  187. ev->event_data.ptrace.process_tgid = task->tgid;
  188. if (ptrace_id == PTRACE_ATTACH) {
  189. ev->event_data.ptrace.tracer_pid = current->pid;
  190. ev->event_data.ptrace.tracer_tgid = current->tgid;
  191. } else if (ptrace_id == PTRACE_DETACH) {
  192. ev->event_data.ptrace.tracer_pid = 0;
  193. ev->event_data.ptrace.tracer_tgid = 0;
  194. } else
  195. return;
  196. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  197. msg->ack = 0; /* not used */
  198. msg->len = sizeof(*ev);
  199. msg->flags = 0; /* not used */
  200. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  201. }
  202. void proc_comm_connector(struct task_struct *task)
  203. {
  204. struct cn_msg *msg;
  205. struct proc_event *ev;
  206. struct timespec ts;
  207. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  208. if (atomic_read(&proc_event_num_listeners) < 1)
  209. return;
  210. msg = buffer_to_cn_msg(buffer);
  211. ev = (struct proc_event *)msg->data;
  212. memset(&ev->event_data, 0, sizeof(ev->event_data));
  213. get_seq(&msg->seq, &ev->cpu);
  214. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  215. ev->timestamp_ns = timespec_to_ns(&ts);
  216. ev->what = PROC_EVENT_COMM;
  217. ev->event_data.comm.process_pid = task->pid;
  218. ev->event_data.comm.process_tgid = task->tgid;
  219. get_task_comm(ev->event_data.comm.comm, task);
  220. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  221. msg->ack = 0; /* not used */
  222. msg->len = sizeof(*ev);
  223. msg->flags = 0; /* not used */
  224. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  225. }
  226. void proc_exit_connector(struct task_struct *task)
  227. {
  228. struct cn_msg *msg;
  229. struct proc_event *ev;
  230. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  231. struct timespec ts;
  232. if (atomic_read(&proc_event_num_listeners) < 1)
  233. return;
  234. msg = buffer_to_cn_msg(buffer);
  235. ev = (struct proc_event*)msg->data;
  236. memset(&ev->event_data, 0, sizeof(ev->event_data));
  237. get_seq(&msg->seq, &ev->cpu);
  238. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  239. ev->timestamp_ns = timespec_to_ns(&ts);
  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. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  246. msg->ack = 0; /* not used */
  247. msg->len = sizeof(*ev);
  248. msg->flags = 0; /* not used */
  249. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  250. }
  251. /*
  252. * Send an acknowledgement message to userspace
  253. *
  254. * Use 0 for success, EFOO otherwise.
  255. * Note: this is the negative of conventional kernel error
  256. * values because it's not being returned via syscall return
  257. * mechanisms.
  258. */
  259. static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
  260. {
  261. struct cn_msg *msg;
  262. struct proc_event *ev;
  263. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  264. struct timespec ts;
  265. if (atomic_read(&proc_event_num_listeners) < 1)
  266. return;
  267. msg = buffer_to_cn_msg(buffer);
  268. ev = (struct proc_event*)msg->data;
  269. memset(&ev->event_data, 0, sizeof(ev->event_data));
  270. msg->seq = rcvd_seq;
  271. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  272. ev->timestamp_ns = timespec_to_ns(&ts);
  273. ev->cpu = -1;
  274. ev->what = PROC_EVENT_NONE;
  275. ev->event_data.ack.err = err;
  276. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  277. msg->ack = rcvd_ack + 1;
  278. msg->len = sizeof(*ev);
  279. msg->flags = 0; /* not used */
  280. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  281. }
  282. /**
  283. * cn_proc_mcast_ctl
  284. * @data: message sent from userspace via the connector
  285. */
  286. static void cn_proc_mcast_ctl(struct cn_msg *msg,
  287. struct netlink_skb_parms *nsp)
  288. {
  289. enum proc_cn_mcast_op *mc_op = NULL;
  290. int err = 0;
  291. if (msg->len != sizeof(*mc_op))
  292. return;
  293. /* Can only change if privileged. */
  294. if (!capable(CAP_NET_ADMIN)) {
  295. err = EPERM;
  296. goto out;
  297. }
  298. mc_op = (enum proc_cn_mcast_op*)msg->data;
  299. switch (*mc_op) {
  300. case PROC_CN_MCAST_LISTEN:
  301. atomic_inc(&proc_event_num_listeners);
  302. break;
  303. case PROC_CN_MCAST_IGNORE:
  304. atomic_dec(&proc_event_num_listeners);
  305. break;
  306. default:
  307. err = EINVAL;
  308. break;
  309. }
  310. out:
  311. cn_proc_ack(err, msg->seq, msg->ack);
  312. }
  313. /*
  314. * cn_proc_init - initialization entry point
  315. *
  316. * Adds the connector callback to the connector driver.
  317. */
  318. static int __init cn_proc_init(void)
  319. {
  320. int err;
  321. if ((err = cn_add_callback(&cn_proc_event_id, "cn_proc",
  322. &cn_proc_mcast_ctl))) {
  323. printk(KERN_WARNING "cn_proc failed to register\n");
  324. return err;
  325. }
  326. return 0;
  327. }
  328. module_init(cn_proc_init);