tty_audit.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/slab.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. buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  30. if (!buf->data)
  31. goto err_buf;
  32. atomic_set(&buf->count, 1);
  33. mutex_init(&buf->mutex);
  34. buf->major = major;
  35. buf->minor = minor;
  36. buf->icanon = icanon;
  37. buf->valid = 0;
  38. return buf;
  39. err_buf:
  40. kfree(buf);
  41. err:
  42. return NULL;
  43. }
  44. static void tty_audit_buf_free(struct tty_audit_buf *buf)
  45. {
  46. WARN_ON(buf->valid != 0);
  47. kfree(buf->data);
  48. kfree(buf);
  49. }
  50. static void tty_audit_buf_put(struct tty_audit_buf *buf)
  51. {
  52. if (atomic_dec_and_test(&buf->count))
  53. tty_audit_buf_free(buf);
  54. }
  55. static void tty_audit_log(const char *description, struct task_struct *tsk,
  56. uid_t loginuid, unsigned sessionid, int major,
  57. int minor, unsigned char *data, size_t size)
  58. {
  59. struct audit_buffer *ab;
  60. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  61. if (ab) {
  62. char name[sizeof(tsk->comm)];
  63. uid_t uid = task_uid(tsk);
  64. audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
  65. "major=%d minor=%d comm=", description,
  66. tsk->pid, uid, loginuid, sessionid,
  67. major, minor);
  68. get_task_comm(name, tsk);
  69. audit_log_untrustedstring(ab, name);
  70. audit_log_format(ab, " data=");
  71. audit_log_n_hex(ab, data, size);
  72. audit_log_end(ab);
  73. }
  74. }
  75. /**
  76. * tty_audit_buf_push - Push buffered data out
  77. *
  78. * Generate an audit message from the contents of @buf, which is owned by
  79. * @tsk with @loginuid. @buf->mutex must be locked.
  80. */
  81. static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
  82. unsigned int sessionid,
  83. struct tty_audit_buf *buf)
  84. {
  85. if (buf->valid == 0)
  86. return;
  87. if (audit_enabled == 0)
  88. return;
  89. tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor,
  90. buf->data, buf->valid);
  91. buf->valid = 0;
  92. }
  93. /**
  94. * tty_audit_buf_push_current - Push buffered data out
  95. *
  96. * Generate an audit message from the contents of @buf, which is owned by
  97. * the current task. @buf->mutex must be locked.
  98. */
  99. static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
  100. {
  101. uid_t auid = audit_get_loginuid(current);
  102. unsigned int sessionid = audit_get_sessionid(current);
  103. tty_audit_buf_push(current, auid, sessionid, buf);
  104. }
  105. /**
  106. * tty_audit_exit - Handle a task exit
  107. *
  108. * Make sure all buffered data is written out and deallocate the buffer.
  109. * Only needs to be called if current->signal->tty_audit_buf != %NULL.
  110. */
  111. void tty_audit_exit(void)
  112. {
  113. struct tty_audit_buf *buf;
  114. spin_lock_irq(&current->sighand->siglock);
  115. buf = current->signal->tty_audit_buf;
  116. current->signal->tty_audit_buf = NULL;
  117. spin_unlock_irq(&current->sighand->siglock);
  118. if (!buf)
  119. return;
  120. mutex_lock(&buf->mutex);
  121. tty_audit_buf_push_current(buf);
  122. mutex_unlock(&buf->mutex);
  123. tty_audit_buf_put(buf);
  124. }
  125. /**
  126. * tty_audit_fork - Copy TTY audit state for a new task
  127. *
  128. * Set up TTY audit state in @sig from current. @sig needs no locking.
  129. */
  130. void tty_audit_fork(struct signal_struct *sig)
  131. {
  132. spin_lock_irq(&current->sighand->siglock);
  133. sig->audit_tty = current->signal->audit_tty;
  134. spin_unlock_irq(&current->sighand->siglock);
  135. }
  136. /**
  137. * tty_audit_tiocsti - Log TIOCSTI
  138. */
  139. void tty_audit_tiocsti(struct tty_struct *tty, char ch)
  140. {
  141. struct tty_audit_buf *buf;
  142. int major, minor, should_audit;
  143. spin_lock_irq(&current->sighand->siglock);
  144. should_audit = current->signal->audit_tty;
  145. buf = current->signal->tty_audit_buf;
  146. if (buf)
  147. atomic_inc(&buf->count);
  148. spin_unlock_irq(&current->sighand->siglock);
  149. major = tty->driver->major;
  150. minor = tty->driver->minor_start + tty->index;
  151. if (buf) {
  152. mutex_lock(&buf->mutex);
  153. if (buf->major == major && buf->minor == minor)
  154. tty_audit_buf_push_current(buf);
  155. mutex_unlock(&buf->mutex);
  156. tty_audit_buf_put(buf);
  157. }
  158. if (should_audit && audit_enabled) {
  159. uid_t auid;
  160. unsigned int sessionid;
  161. auid = audit_get_loginuid(current);
  162. sessionid = audit_get_sessionid(current);
  163. tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
  164. minor, &ch, 1);
  165. }
  166. }
  167. /**
  168. * tty_audit_push_task - Flush task's pending audit data
  169. */
  170. void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
  171. {
  172. struct tty_audit_buf *buf;
  173. spin_lock_irq(&tsk->sighand->siglock);
  174. buf = tsk->signal->tty_audit_buf;
  175. if (buf)
  176. atomic_inc(&buf->count);
  177. spin_unlock_irq(&tsk->sighand->siglock);
  178. if (!buf)
  179. return;
  180. mutex_lock(&buf->mutex);
  181. tty_audit_buf_push(tsk, loginuid, sessionid, buf);
  182. mutex_unlock(&buf->mutex);
  183. tty_audit_buf_put(buf);
  184. }
  185. /**
  186. * tty_audit_buf_get - Get an audit buffer.
  187. *
  188. * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
  189. * if TTY auditing is disabled or out of memory. Otherwise, return a new
  190. * reference to the buffer.
  191. */
  192. static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
  193. {
  194. struct tty_audit_buf *buf, *buf2;
  195. buf = NULL;
  196. buf2 = NULL;
  197. spin_lock_irq(&current->sighand->siglock);
  198. if (likely(!current->signal->audit_tty))
  199. goto out;
  200. buf = current->signal->tty_audit_buf;
  201. if (buf) {
  202. atomic_inc(&buf->count);
  203. goto out;
  204. }
  205. spin_unlock_irq(&current->sighand->siglock);
  206. buf2 = tty_audit_buf_alloc(tty->driver->major,
  207. tty->driver->minor_start + tty->index,
  208. tty->icanon);
  209. if (buf2 == NULL) {
  210. audit_log_lost("out of memory in TTY auditing");
  211. return NULL;
  212. }
  213. spin_lock_irq(&current->sighand->siglock);
  214. if (!current->signal->audit_tty)
  215. goto out;
  216. buf = current->signal->tty_audit_buf;
  217. if (!buf) {
  218. current->signal->tty_audit_buf = buf2;
  219. buf = buf2;
  220. buf2 = NULL;
  221. }
  222. atomic_inc(&buf->count);
  223. /* Fall through */
  224. out:
  225. spin_unlock_irq(&current->sighand->siglock);
  226. if (buf2)
  227. tty_audit_buf_free(buf2);
  228. return buf;
  229. }
  230. /**
  231. * tty_audit_add_data - Add data for TTY auditing.
  232. *
  233. * Audit @data of @size from @tty, if necessary.
  234. */
  235. void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  236. size_t size)
  237. {
  238. struct tty_audit_buf *buf;
  239. int major, minor;
  240. if (unlikely(size == 0))
  241. return;
  242. if (tty->driver->type == TTY_DRIVER_TYPE_PTY
  243. && tty->driver->subtype == PTY_TYPE_MASTER)
  244. return;
  245. buf = tty_audit_buf_get(tty);
  246. if (!buf)
  247. return;
  248. mutex_lock(&buf->mutex);
  249. major = tty->driver->major;
  250. minor = tty->driver->minor_start + tty->index;
  251. if (buf->major != major || buf->minor != minor
  252. || buf->icanon != tty->icanon) {
  253. tty_audit_buf_push_current(buf);
  254. buf->major = major;
  255. buf->minor = minor;
  256. buf->icanon = tty->icanon;
  257. }
  258. do {
  259. size_t run;
  260. run = N_TTY_BUF_SIZE - buf->valid;
  261. if (run > size)
  262. run = size;
  263. memcpy(buf->data + buf->valid, data, run);
  264. buf->valid += run;
  265. data += run;
  266. size -= run;
  267. if (buf->valid == N_TTY_BUF_SIZE)
  268. tty_audit_buf_push_current(buf);
  269. } while (size != 0);
  270. mutex_unlock(&buf->mutex);
  271. tty_audit_buf_put(buf);
  272. }
  273. /**
  274. * tty_audit_push - Push buffered data out
  275. *
  276. * Make sure no audit data is pending for @tty on the current process.
  277. */
  278. void tty_audit_push(struct tty_struct *tty)
  279. {
  280. struct tty_audit_buf *buf;
  281. spin_lock_irq(&current->sighand->siglock);
  282. if (likely(!current->signal->audit_tty)) {
  283. spin_unlock_irq(&current->sighand->siglock);
  284. return;
  285. }
  286. buf = current->signal->tty_audit_buf;
  287. if (buf)
  288. atomic_inc(&buf->count);
  289. spin_unlock_irq(&current->sighand->siglock);
  290. if (buf) {
  291. int major, minor;
  292. major = tty->driver->major;
  293. minor = tty->driver->minor_start + tty->index;
  294. mutex_lock(&buf->mutex);
  295. if (buf->major == major && buf->minor == minor)
  296. tty_audit_buf_push_current(buf);
  297. mutex_unlock(&buf->mutex);
  298. tty_audit_buf_put(buf);
  299. }
  300. }