tty_audit.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Creating audit events from TTY input.
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All rights reserved. This copyrighted
  5. * material is made available to anyone wishing to use, modify, copy, or
  6. * redistribute it subject to the terms and conditions of the GNU General
  7. * Public License v.2.
  8. *
  9. * Authors: Miloslav Trmac <mitr@redhat.com>
  10. */
  11. #include <linux/audit.h>
  12. #include <linux/file.h>
  13. #include <linux/tty.h>
  14. struct tty_audit_buf {
  15. atomic_t count;
  16. struct mutex mutex; /* Protects all data below */
  17. int major, minor; /* The TTY which the data is from */
  18. unsigned icanon:1;
  19. size_t valid;
  20. unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
  21. };
  22. static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
  23. int icanon)
  24. {
  25. struct tty_audit_buf *buf;
  26. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  27. if (!buf)
  28. goto err;
  29. if (PAGE_SIZE != N_TTY_BUF_SIZE)
  30. buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  31. else
  32. buf->data = (unsigned char *)__get_free_page(GFP_KERNEL);
  33. if (!buf->data)
  34. goto err_buf;
  35. atomic_set(&buf->count, 1);
  36. mutex_init(&buf->mutex);
  37. buf->major = major;
  38. buf->minor = minor;
  39. buf->icanon = icanon;
  40. buf->valid = 0;
  41. return buf;
  42. err_buf:
  43. kfree(buf);
  44. err:
  45. return NULL;
  46. }
  47. static void tty_audit_buf_free(struct tty_audit_buf *buf)
  48. {
  49. WARN_ON(buf->valid != 0);
  50. if (PAGE_SIZE != N_TTY_BUF_SIZE)
  51. kfree(buf->data);
  52. else
  53. free_page((unsigned long)buf->data);
  54. kfree(buf);
  55. }
  56. static void tty_audit_buf_put(struct tty_audit_buf *buf)
  57. {
  58. if (atomic_dec_and_test(&buf->count))
  59. tty_audit_buf_free(buf);
  60. }
  61. /**
  62. * tty_audit_buf_push - Push buffered data out
  63. *
  64. * Generate an audit message from the contents of @buf, which is owned by
  65. * @tsk with @loginuid. @buf->mutex must be locked.
  66. */
  67. static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
  68. unsigned int sessionid,
  69. struct tty_audit_buf *buf)
  70. {
  71. struct audit_buffer *ab;
  72. if (buf->valid == 0)
  73. return;
  74. if (audit_enabled == 0)
  75. return;
  76. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  77. if (ab) {
  78. char name[sizeof(tsk->comm)];
  79. audit_log_format(ab, "tty pid=%u uid=%u auid=%u ses=%u "
  80. "major=%d minor=%d comm=", tsk->pid, tsk->uid,
  81. loginuid, sessionid, buf->major, buf->minor);
  82. get_task_comm(name, tsk);
  83. audit_log_untrustedstring(ab, name);
  84. audit_log_format(ab, " data=");
  85. audit_log_n_untrustedstring(ab, buf->valid, buf->data);
  86. audit_log_end(ab);
  87. }
  88. buf->valid = 0;
  89. }
  90. /**
  91. * tty_audit_buf_push_current - Push buffered data out
  92. *
  93. * Generate an audit message from the contents of @buf, which is owned by
  94. * the current task. @buf->mutex must be locked.
  95. */
  96. static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
  97. {
  98. uid_t auid = audit_get_loginuid(current);
  99. unsigned int sessionid = audit_get_sessionid(current);
  100. tty_audit_buf_push(current, auid, sessionid, buf);
  101. }
  102. /**
  103. * tty_audit_exit - Handle a task exit
  104. *
  105. * Make sure all buffered data is written out and deallocate the buffer.
  106. * Only needs to be called if current->signal->tty_audit_buf != %NULL.
  107. */
  108. void tty_audit_exit(void)
  109. {
  110. struct tty_audit_buf *buf;
  111. spin_lock_irq(&current->sighand->siglock);
  112. buf = current->signal->tty_audit_buf;
  113. current->signal->tty_audit_buf = NULL;
  114. spin_unlock_irq(&current->sighand->siglock);
  115. if (!buf)
  116. return;
  117. mutex_lock(&buf->mutex);
  118. tty_audit_buf_push_current(buf);
  119. mutex_unlock(&buf->mutex);
  120. tty_audit_buf_put(buf);
  121. }
  122. /**
  123. * tty_audit_fork - Copy TTY audit state for a new task
  124. *
  125. * Set up TTY audit state in @sig from current. @sig needs no locking.
  126. */
  127. void tty_audit_fork(struct signal_struct *sig)
  128. {
  129. spin_lock_irq(&current->sighand->siglock);
  130. sig->audit_tty = current->signal->audit_tty;
  131. spin_unlock_irq(&current->sighand->siglock);
  132. sig->tty_audit_buf = NULL;
  133. }
  134. /**
  135. * tty_audit_push_task - Flush task's pending audit data
  136. */
  137. void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid)
  138. {
  139. struct tty_audit_buf *buf;
  140. /* FIXME I think this is correct. Check against netlink once that is
  141. * I really need to read this code more closely. But that's for
  142. * another patch.
  143. */
  144. unsigned int sessionid = audit_get_sessionid(tsk);
  145. spin_lock_irq(&tsk->sighand->siglock);
  146. buf = tsk->signal->tty_audit_buf;
  147. if (buf)
  148. atomic_inc(&buf->count);
  149. spin_unlock_irq(&tsk->sighand->siglock);
  150. if (!buf)
  151. return;
  152. mutex_lock(&buf->mutex);
  153. tty_audit_buf_push(tsk, loginuid, sessionid, buf);
  154. mutex_unlock(&buf->mutex);
  155. tty_audit_buf_put(buf);
  156. }
  157. /**
  158. * tty_audit_buf_get - Get an audit buffer.
  159. *
  160. * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
  161. * if TTY auditing is disabled or out of memory. Otherwise, return a new
  162. * reference to the buffer.
  163. */
  164. static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
  165. {
  166. struct tty_audit_buf *buf, *buf2;
  167. buf = NULL;
  168. buf2 = NULL;
  169. spin_lock_irq(&current->sighand->siglock);
  170. if (likely(!current->signal->audit_tty))
  171. goto out;
  172. buf = current->signal->tty_audit_buf;
  173. if (buf) {
  174. atomic_inc(&buf->count);
  175. goto out;
  176. }
  177. spin_unlock_irq(&current->sighand->siglock);
  178. buf2 = tty_audit_buf_alloc(tty->driver->major,
  179. tty->driver->minor_start + tty->index,
  180. tty->icanon);
  181. if (buf2 == NULL) {
  182. audit_log_lost("out of memory in TTY auditing");
  183. return NULL;
  184. }
  185. spin_lock_irq(&current->sighand->siglock);
  186. if (!current->signal->audit_tty)
  187. goto out;
  188. buf = current->signal->tty_audit_buf;
  189. if (!buf) {
  190. current->signal->tty_audit_buf = buf2;
  191. buf = buf2;
  192. buf2 = NULL;
  193. }
  194. atomic_inc(&buf->count);
  195. /* Fall through */
  196. out:
  197. spin_unlock_irq(&current->sighand->siglock);
  198. if (buf2)
  199. tty_audit_buf_free(buf2);
  200. return buf;
  201. }
  202. /**
  203. * tty_audit_add_data - Add data for TTY auditing.
  204. *
  205. * Audit @data of @size from @tty, if necessary.
  206. */
  207. void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  208. size_t size)
  209. {
  210. struct tty_audit_buf *buf;
  211. int major, minor;
  212. if (unlikely(size == 0))
  213. return;
  214. buf = tty_audit_buf_get(tty);
  215. if (!buf)
  216. return;
  217. mutex_lock(&buf->mutex);
  218. major = tty->driver->major;
  219. minor = tty->driver->minor_start + tty->index;
  220. if (buf->major != major || buf->minor != minor
  221. || buf->icanon != tty->icanon) {
  222. tty_audit_buf_push_current(buf);
  223. buf->major = major;
  224. buf->minor = minor;
  225. buf->icanon = tty->icanon;
  226. }
  227. do {
  228. size_t run;
  229. run = N_TTY_BUF_SIZE - buf->valid;
  230. if (run > size)
  231. run = size;
  232. memcpy(buf->data + buf->valid, data, run);
  233. buf->valid += run;
  234. data += run;
  235. size -= run;
  236. if (buf->valid == N_TTY_BUF_SIZE)
  237. tty_audit_buf_push_current(buf);
  238. } while (size != 0);
  239. mutex_unlock(&buf->mutex);
  240. tty_audit_buf_put(buf);
  241. }
  242. /**
  243. * tty_audit_push - Push buffered data out
  244. *
  245. * Make sure no audit data is pending for @tty on the current process.
  246. */
  247. void tty_audit_push(struct tty_struct *tty)
  248. {
  249. struct tty_audit_buf *buf;
  250. spin_lock_irq(&current->sighand->siglock);
  251. if (likely(!current->signal->audit_tty)) {
  252. spin_unlock_irq(&current->sighand->siglock);
  253. return;
  254. }
  255. buf = current->signal->tty_audit_buf;
  256. if (buf)
  257. atomic_inc(&buf->count);
  258. spin_unlock_irq(&current->sighand->siglock);
  259. if (buf) {
  260. int major, minor;
  261. major = tty->driver->major;
  262. minor = tty->driver->minor_start + tty->index;
  263. mutex_lock(&buf->mutex);
  264. if (buf->major == major && buf->minor == minor)
  265. tty_audit_buf_push_current(buf);
  266. mutex_unlock(&buf->mutex);
  267. tty_audit_buf_put(buf);
  268. }
  269. }
  270. /**
  271. * tty_audit_opening - A TTY is being opened.
  272. *
  273. * As a special hack, tasks that close all their TTYs and open new ones
  274. * are assumed to be system daemons (e.g. getty) and auditing is
  275. * automatically disabled for them.
  276. */
  277. void tty_audit_opening(void)
  278. {
  279. int disable;
  280. disable = 1;
  281. spin_lock_irq(&current->sighand->siglock);
  282. if (current->signal->audit_tty == 0)
  283. disable = 0;
  284. spin_unlock_irq(&current->sighand->siglock);
  285. if (!disable)
  286. return;
  287. task_lock(current);
  288. if (current->files) {
  289. struct fdtable *fdt;
  290. unsigned i;
  291. /*
  292. * We don't take a ref to the file, so we must hold ->file_lock
  293. * instead.
  294. */
  295. spin_lock(&current->files->file_lock);
  296. fdt = files_fdtable(current->files);
  297. for (i = 0; i < fdt->max_fds; i++) {
  298. struct file *filp;
  299. filp = fcheck_files(current->files, i);
  300. if (filp && is_tty(filp)) {
  301. disable = 0;
  302. break;
  303. }
  304. }
  305. spin_unlock(&current->files->file_lock);
  306. }
  307. task_unlock(current);
  308. if (!disable)
  309. return;
  310. spin_lock_irq(&current->sighand->siglock);
  311. current->signal->audit_tty = 0;
  312. spin_unlock_irq(&current->sighand->siglock);
  313. }