cn_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/kernel.h>
  25. #include <linux/ktime.h>
  26. #include <linux/init.h>
  27. #include <linux/connector.h>
  28. #include <linux/gfp.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/atomic.h>
  31. #include <linux/pid_namespace.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 send_msg(struct cn_msg *msg)
  52. {
  53. preempt_disable();
  54. msg->seq = __this_cpu_inc_return(proc_event_counts) - 1;
  55. ((struct proc_event *)msg->data)->cpu = smp_processor_id();
  56. /*
  57. * Preemption remains disabled during send to ensure the messages are
  58. * ordered according to their sequence numbers.
  59. *
  60. * If cn_netlink_send() fails, the data is not sent.
  61. */
  62. cn_netlink_send(msg, 0, CN_IDX_PROC, GFP_NOWAIT);
  63. preempt_enable();
  64. }
  65. void proc_fork_connector(struct task_struct *task)
  66. {
  67. struct cn_msg *msg;
  68. struct proc_event *ev;
  69. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  70. struct task_struct *parent;
  71. if (atomic_read(&proc_event_num_listeners) < 1)
  72. return;
  73. msg = buffer_to_cn_msg(buffer);
  74. ev = (struct proc_event *)msg->data;
  75. memset(&ev->event_data, 0, sizeof(ev->event_data));
  76. ev->timestamp_ns = ktime_get_ns();
  77. ev->what = PROC_EVENT_FORK;
  78. rcu_read_lock();
  79. parent = rcu_dereference(task->real_parent);
  80. ev->event_data.fork.parent_pid = parent->pid;
  81. ev->event_data.fork.parent_tgid = parent->tgid;
  82. rcu_read_unlock();
  83. ev->event_data.fork.child_pid = task->pid;
  84. ev->event_data.fork.child_tgid = task->tgid;
  85. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  86. msg->ack = 0; /* not used */
  87. msg->len = sizeof(*ev);
  88. msg->flags = 0; /* not used */
  89. send_msg(msg);
  90. }
  91. void proc_exec_connector(struct task_struct *task)
  92. {
  93. struct cn_msg *msg;
  94. struct proc_event *ev;
  95. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  96. if (atomic_read(&proc_event_num_listeners) < 1)
  97. return;
  98. msg = buffer_to_cn_msg(buffer);
  99. ev = (struct proc_event *)msg->data;
  100. memset(&ev->event_data, 0, sizeof(ev->event_data));
  101. ev->timestamp_ns = ktime_get_ns();
  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. send_msg(msg);
  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. const struct cred *cred;
  117. if (atomic_read(&proc_event_num_listeners) < 1)
  118. return;
  119. msg = buffer_to_cn_msg(buffer);
  120. ev = (struct proc_event *)msg->data;
  121. memset(&ev->event_data, 0, sizeof(ev->event_data));
  122. ev->what = which_id;
  123. ev->event_data.id.process_pid = task->pid;
  124. ev->event_data.id.process_tgid = task->tgid;
  125. rcu_read_lock();
  126. cred = __task_cred(task);
  127. if (which_id == PROC_EVENT_UID) {
  128. ev->event_data.id.r.ruid = from_kuid_munged(&init_user_ns, cred->uid);
  129. ev->event_data.id.e.euid = from_kuid_munged(&init_user_ns, cred->euid);
  130. } else if (which_id == PROC_EVENT_GID) {
  131. ev->event_data.id.r.rgid = from_kgid_munged(&init_user_ns, cred->gid);
  132. ev->event_data.id.e.egid = from_kgid_munged(&init_user_ns, cred->egid);
  133. } else {
  134. rcu_read_unlock();
  135. return;
  136. }
  137. rcu_read_unlock();
  138. ev->timestamp_ns = ktime_get_ns();
  139. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  140. msg->ack = 0; /* not used */
  141. msg->len = sizeof(*ev);
  142. msg->flags = 0; /* not used */
  143. send_msg(msg);
  144. }
  145. void proc_sid_connector(struct task_struct *task)
  146. {
  147. struct cn_msg *msg;
  148. struct proc_event *ev;
  149. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  150. if (atomic_read(&proc_event_num_listeners) < 1)
  151. return;
  152. msg = buffer_to_cn_msg(buffer);
  153. ev = (struct proc_event *)msg->data;
  154. memset(&ev->event_data, 0, sizeof(ev->event_data));
  155. ev->timestamp_ns = ktime_get_ns();
  156. ev->what = PROC_EVENT_SID;
  157. ev->event_data.sid.process_pid = task->pid;
  158. ev->event_data.sid.process_tgid = task->tgid;
  159. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  160. msg->ack = 0; /* not used */
  161. msg->len = sizeof(*ev);
  162. msg->flags = 0; /* not used */
  163. send_msg(msg);
  164. }
  165. void proc_ptrace_connector(struct task_struct *task, int ptrace_id)
  166. {
  167. struct cn_msg *msg;
  168. struct proc_event *ev;
  169. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  170. if (atomic_read(&proc_event_num_listeners) < 1)
  171. return;
  172. msg = buffer_to_cn_msg(buffer);
  173. ev = (struct proc_event *)msg->data;
  174. memset(&ev->event_data, 0, sizeof(ev->event_data));
  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. send_msg(msg);
  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. ev->timestamp_ns = ktime_get_ns();
  204. ev->what = PROC_EVENT_COMM;
  205. ev->event_data.comm.process_pid = task->pid;
  206. ev->event_data.comm.process_tgid = task->tgid;
  207. get_task_comm(ev->event_data.comm.comm, task);
  208. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  209. msg->ack = 0; /* not used */
  210. msg->len = sizeof(*ev);
  211. msg->flags = 0; /* not used */
  212. send_msg(msg);
  213. }
  214. void proc_coredump_connector(struct task_struct *task)
  215. {
  216. struct cn_msg *msg;
  217. struct proc_event *ev;
  218. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  219. if (atomic_read(&proc_event_num_listeners) < 1)
  220. return;
  221. msg = buffer_to_cn_msg(buffer);
  222. ev = (struct proc_event *)msg->data;
  223. memset(&ev->event_data, 0, sizeof(ev->event_data));
  224. ev->timestamp_ns = ktime_get_ns();
  225. ev->what = PROC_EVENT_COREDUMP;
  226. ev->event_data.coredump.process_pid = task->pid;
  227. ev->event_data.coredump.process_tgid = task->tgid;
  228. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  229. msg->ack = 0; /* not used */
  230. msg->len = sizeof(*ev);
  231. msg->flags = 0; /* not used */
  232. send_msg(msg);
  233. }
  234. void proc_exit_connector(struct task_struct *task)
  235. {
  236. struct cn_msg *msg;
  237. struct proc_event *ev;
  238. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  239. if (atomic_read(&proc_event_num_listeners) < 1)
  240. return;
  241. msg = buffer_to_cn_msg(buffer);
  242. ev = (struct proc_event *)msg->data;
  243. memset(&ev->event_data, 0, sizeof(ev->event_data));
  244. ev->timestamp_ns = ktime_get_ns();
  245. ev->what = PROC_EVENT_EXIT;
  246. ev->event_data.exit.process_pid = task->pid;
  247. ev->event_data.exit.process_tgid = task->tgid;
  248. ev->event_data.exit.exit_code = task->exit_code;
  249. ev->event_data.exit.exit_signal = task->exit_signal;
  250. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  251. msg->ack = 0; /* not used */
  252. msg->len = sizeof(*ev);
  253. msg->flags = 0; /* not used */
  254. send_msg(msg);
  255. }
  256. /*
  257. * Send an acknowledgement message to userspace
  258. *
  259. * Use 0 for success, EFOO otherwise.
  260. * Note: this is the negative of conventional kernel error
  261. * values because it's not being returned via syscall return
  262. * mechanisms.
  263. */
  264. static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
  265. {
  266. struct cn_msg *msg;
  267. struct proc_event *ev;
  268. __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  269. if (atomic_read(&proc_event_num_listeners) < 1)
  270. return;
  271. msg = buffer_to_cn_msg(buffer);
  272. ev = (struct proc_event *)msg->data;
  273. memset(&ev->event_data, 0, sizeof(ev->event_data));
  274. msg->seq = rcvd_seq;
  275. ev->timestamp_ns = ktime_get_ns();
  276. ev->cpu = -1;
  277. ev->what = PROC_EVENT_NONE;
  278. ev->event_data.ack.err = err;
  279. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  280. msg->ack = rcvd_ack + 1;
  281. msg->len = sizeof(*ev);
  282. msg->flags = 0; /* not used */
  283. send_msg(msg);
  284. }
  285. /**
  286. * cn_proc_mcast_ctl
  287. * @data: message sent from userspace via the connector
  288. */
  289. static void cn_proc_mcast_ctl(struct cn_msg *msg,
  290. struct netlink_skb_parms *nsp)
  291. {
  292. enum proc_cn_mcast_op *mc_op = NULL;
  293. int err = 0;
  294. if (msg->len != sizeof(*mc_op))
  295. return;
  296. /*
  297. * Events are reported with respect to the initial pid
  298. * and user namespaces so ignore requestors from
  299. * other namespaces.
  300. */
  301. if ((current_user_ns() != &init_user_ns) ||
  302. (task_active_pid_ns(current) != &init_pid_ns))
  303. return;
  304. /* Can only change if privileged. */
  305. if (!__netlink_ns_capable(nsp, &init_user_ns, CAP_NET_ADMIN)) {
  306. err = EPERM;
  307. goto out;
  308. }
  309. mc_op = (enum proc_cn_mcast_op *)msg->data;
  310. switch (*mc_op) {
  311. case PROC_CN_MCAST_LISTEN:
  312. atomic_inc(&proc_event_num_listeners);
  313. break;
  314. case PROC_CN_MCAST_IGNORE:
  315. atomic_dec(&proc_event_num_listeners);
  316. break;
  317. default:
  318. err = EINVAL;
  319. break;
  320. }
  321. out:
  322. cn_proc_ack(err, msg->seq, msg->ack);
  323. }
  324. /*
  325. * cn_proc_init - initialization entry point
  326. *
  327. * Adds the connector callback to the connector driver.
  328. */
  329. static int __init cn_proc_init(void)
  330. {
  331. int err = cn_add_callback(&cn_proc_event_id,
  332. "cn_proc",
  333. &cn_proc_mcast_ctl);
  334. if (err) {
  335. pr_warn("cn_proc failed to register\n");
  336. return err;
  337. }
  338. return 0;
  339. }
  340. device_initcall(cn_proc_init);