tty_audit.c 7.9 KB

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