tty_audit.c 7.9 KB

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