tty_audit.c 8.0 KB

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