tty_audit.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. struct tty_audit_buf *buf)
  69. {
  70. struct audit_buffer *ab;
  71. if (buf->valid == 0)
  72. return;
  73. if (audit_enabled == 0)
  74. return;
  75. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  76. if (ab) {
  77. char name[sizeof(tsk->comm)];
  78. audit_log_format(ab, "tty pid=%u uid=%u auid=%u major=%d "
  79. "minor=%d comm=", tsk->pid, tsk->uid,
  80. loginuid, buf->major, buf->minor);
  81. get_task_comm(name, tsk);
  82. audit_log_untrustedstring(ab, name);
  83. audit_log_format(ab, " data=");
  84. audit_log_n_untrustedstring(ab, buf->valid, buf->data);
  85. audit_log_end(ab);
  86. }
  87. buf->valid = 0;
  88. }
  89. /**
  90. * tty_audit_buf_push_current - Push buffered data out
  91. *
  92. * Generate an audit message from the contents of @buf, which is owned by
  93. * the current task. @buf->mutex must be locked.
  94. */
  95. static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
  96. {
  97. tty_audit_buf_push(current, audit_get_loginuid(current), buf);
  98. }
  99. /**
  100. * tty_audit_exit - Handle a task exit
  101. *
  102. * Make sure all buffered data is written out and deallocate the buffer.
  103. * Only needs to be called if current->signal->tty_audit_buf != %NULL.
  104. */
  105. void tty_audit_exit(void)
  106. {
  107. struct tty_audit_buf *buf;
  108. spin_lock_irq(&current->sighand->siglock);
  109. buf = current->signal->tty_audit_buf;
  110. current->signal->tty_audit_buf = NULL;
  111. spin_unlock_irq(&current->sighand->siglock);
  112. if (!buf)
  113. return;
  114. mutex_lock(&buf->mutex);
  115. tty_audit_buf_push_current(buf);
  116. mutex_unlock(&buf->mutex);
  117. tty_audit_buf_put(buf);
  118. }
  119. /**
  120. * tty_audit_fork - Copy TTY audit state for a new task
  121. *
  122. * Set up TTY audit state in @sig from current. @sig needs no locking.
  123. */
  124. void tty_audit_fork(struct signal_struct *sig)
  125. {
  126. spin_lock_irq(&current->sighand->siglock);
  127. sig->audit_tty = current->signal->audit_tty;
  128. spin_unlock_irq(&current->sighand->siglock);
  129. sig->tty_audit_buf = NULL;
  130. }
  131. /**
  132. * tty_audit_push_task - Flush task's pending audit data
  133. */
  134. void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid)
  135. {
  136. struct tty_audit_buf *buf;
  137. spin_lock_irq(&tsk->sighand->siglock);
  138. buf = tsk->signal->tty_audit_buf;
  139. if (buf)
  140. atomic_inc(&buf->count);
  141. spin_unlock_irq(&tsk->sighand->siglock);
  142. if (!buf)
  143. return;
  144. mutex_lock(&buf->mutex);
  145. tty_audit_buf_push(tsk, loginuid, buf);
  146. mutex_unlock(&buf->mutex);
  147. tty_audit_buf_put(buf);
  148. }
  149. /**
  150. * tty_audit_buf_get - Get an audit buffer.
  151. *
  152. * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
  153. * if TTY auditing is disabled or out of memory. Otherwise, return a new
  154. * reference to the buffer.
  155. */
  156. static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
  157. {
  158. struct tty_audit_buf *buf, *buf2;
  159. buf = NULL;
  160. buf2 = NULL;
  161. spin_lock_irq(&current->sighand->siglock);
  162. if (likely(!current->signal->audit_tty))
  163. goto out;
  164. buf = current->signal->tty_audit_buf;
  165. if (buf) {
  166. atomic_inc(&buf->count);
  167. goto out;
  168. }
  169. spin_unlock_irq(&current->sighand->siglock);
  170. buf2 = tty_audit_buf_alloc(tty->driver->major,
  171. tty->driver->minor_start + tty->index,
  172. tty->icanon);
  173. if (buf2 == NULL) {
  174. audit_log_lost("out of memory in TTY auditing");
  175. return NULL;
  176. }
  177. spin_lock_irq(&current->sighand->siglock);
  178. if (!current->signal->audit_tty)
  179. goto out;
  180. buf = current->signal->tty_audit_buf;
  181. if (!buf) {
  182. current->signal->tty_audit_buf = buf2;
  183. buf = buf2;
  184. buf2 = NULL;
  185. }
  186. atomic_inc(&buf->count);
  187. /* Fall through */
  188. out:
  189. spin_unlock_irq(&current->sighand->siglock);
  190. if (buf2)
  191. tty_audit_buf_free(buf2);
  192. return buf;
  193. }
  194. /**
  195. * tty_audit_add_data - Add data for TTY auditing.
  196. *
  197. * Audit @data of @size from @tty, if necessary.
  198. */
  199. void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  200. size_t size)
  201. {
  202. struct tty_audit_buf *buf;
  203. int major, minor;
  204. if (unlikely(size == 0))
  205. return;
  206. buf = tty_audit_buf_get(tty);
  207. if (!buf)
  208. return;
  209. mutex_lock(&buf->mutex);
  210. major = tty->driver->major;
  211. minor = tty->driver->minor_start + tty->index;
  212. if (buf->major != major || buf->minor != minor
  213. || buf->icanon != tty->icanon) {
  214. tty_audit_buf_push_current(buf);
  215. buf->major = major;
  216. buf->minor = minor;
  217. buf->icanon = tty->icanon;
  218. }
  219. do {
  220. size_t run;
  221. run = N_TTY_BUF_SIZE - buf->valid;
  222. if (run > size)
  223. run = size;
  224. memcpy(buf->data + buf->valid, data, run);
  225. buf->valid += run;
  226. data += run;
  227. size -= run;
  228. if (buf->valid == N_TTY_BUF_SIZE)
  229. tty_audit_buf_push_current(buf);
  230. } while (size != 0);
  231. mutex_unlock(&buf->mutex);
  232. tty_audit_buf_put(buf);
  233. }
  234. /**
  235. * tty_audit_push - Push buffered data out
  236. *
  237. * Make sure no audit data is pending for @tty on the current process.
  238. */
  239. void tty_audit_push(struct tty_struct *tty)
  240. {
  241. struct tty_audit_buf *buf;
  242. spin_lock_irq(&current->sighand->siglock);
  243. if (likely(!current->signal->audit_tty)) {
  244. spin_unlock_irq(&current->sighand->siglock);
  245. return;
  246. }
  247. buf = current->signal->tty_audit_buf;
  248. if (buf)
  249. atomic_inc(&buf->count);
  250. spin_unlock_irq(&current->sighand->siglock);
  251. if (buf) {
  252. int major, minor;
  253. major = tty->driver->major;
  254. minor = tty->driver->minor_start + tty->index;
  255. mutex_lock(&buf->mutex);
  256. if (buf->major == major && buf->minor == minor)
  257. tty_audit_buf_push_current(buf);
  258. mutex_unlock(&buf->mutex);
  259. tty_audit_buf_put(buf);
  260. }
  261. }
  262. /**
  263. * tty_audit_opening - A TTY is being opened.
  264. *
  265. * As a special hack, tasks that close all their TTYs and open new ones
  266. * are assumed to be system daemons (e.g. getty) and auditing is
  267. * automatically disabled for them.
  268. */
  269. void tty_audit_opening(void)
  270. {
  271. int disable;
  272. disable = 1;
  273. spin_lock_irq(&current->sighand->siglock);
  274. if (current->signal->audit_tty == 0)
  275. disable = 0;
  276. spin_unlock_irq(&current->sighand->siglock);
  277. if (!disable)
  278. return;
  279. task_lock(current);
  280. if (current->files) {
  281. struct fdtable *fdt;
  282. unsigned i;
  283. /*
  284. * We don't take a ref to the file, so we must hold ->file_lock
  285. * instead.
  286. */
  287. spin_lock(&current->files->file_lock);
  288. fdt = files_fdtable(current->files);
  289. for (i = 0; i < fdt->max_fds; i++) {
  290. struct file *filp;
  291. filp = fcheck_files(current->files, i);
  292. if (filp && is_tty(filp)) {
  293. disable = 0;
  294. break;
  295. }
  296. }
  297. spin_unlock(&current->files->file_lock);
  298. }
  299. task_unlock(current);
  300. if (!disable)
  301. return;
  302. spin_lock_irq(&current->sighand->siglock);
  303. current->signal->audit_tty = 0;
  304. spin_unlock_irq(&current->sighand->siglock);
  305. }