tty_audit.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. unsigned long flags;
  103. spin_lock_irqsave(&current->sighand->siglock, flags);
  104. buf = current->signal->tty_audit_buf;
  105. current->signal->tty_audit_buf = NULL;
  106. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  107. if (!buf)
  108. return;
  109. mutex_lock(&buf->mutex);
  110. tty_audit_buf_push(buf);
  111. mutex_unlock(&buf->mutex);
  112. tty_audit_buf_put(buf);
  113. }
  114. /**
  115. * tty_audit_fork - Copy TTY audit state for a new task
  116. *
  117. * Set up TTY audit state in @sig from current. @sig needs no locking.
  118. */
  119. void tty_audit_fork(struct signal_struct *sig)
  120. {
  121. unsigned long flags;
  122. spin_lock_irqsave(&current->sighand->siglock, flags);
  123. sig->audit_tty = current->signal->audit_tty;
  124. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  125. }
  126. /**
  127. * tty_audit_tiocsti - Log TIOCSTI
  128. */
  129. void tty_audit_tiocsti(struct tty_struct *tty, char ch)
  130. {
  131. struct tty_audit_buf *buf;
  132. int major, minor, should_audit;
  133. unsigned long flags;
  134. spin_lock_irqsave(&current->sighand->siglock, flags);
  135. should_audit = current->signal->audit_tty;
  136. buf = current->signal->tty_audit_buf;
  137. if (buf)
  138. atomic_inc(&buf->count);
  139. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  140. major = tty->driver->major;
  141. minor = tty->driver->minor_start + tty->index;
  142. if (buf) {
  143. mutex_lock(&buf->mutex);
  144. if (buf->major == major && buf->minor == minor)
  145. tty_audit_buf_push(buf);
  146. mutex_unlock(&buf->mutex);
  147. tty_audit_buf_put(buf);
  148. }
  149. if (should_audit && audit_enabled) {
  150. kuid_t auid;
  151. unsigned int sessionid;
  152. auid = audit_get_loginuid(current);
  153. sessionid = audit_get_sessionid(current);
  154. tty_audit_log("ioctl=TIOCSTI", major, minor, &ch, 1);
  155. }
  156. }
  157. /**
  158. * tty_audit_push_current - Flush current's pending audit data
  159. *
  160. * Try to lock sighand and get a reference to the tty audit buffer if available.
  161. * Flush the buffer or return an appropriate error code.
  162. */
  163. int tty_audit_push_current(void)
  164. {
  165. struct tty_audit_buf *buf = ERR_PTR(-EPERM);
  166. struct task_struct *tsk = current;
  167. unsigned long flags;
  168. if (!lock_task_sighand(tsk, &flags))
  169. return -ESRCH;
  170. if (tsk->signal->audit_tty) {
  171. buf = tsk->signal->tty_audit_buf;
  172. if (buf)
  173. atomic_inc(&buf->count);
  174. }
  175. unlock_task_sighand(tsk, &flags);
  176. /*
  177. * Return 0 when signal->audit_tty set
  178. * but tsk->signal->tty_audit_buf == NULL.
  179. */
  180. if (!buf || IS_ERR(buf))
  181. return PTR_ERR(buf);
  182. mutex_lock(&buf->mutex);
  183. tty_audit_buf_push(buf);
  184. mutex_unlock(&buf->mutex);
  185. tty_audit_buf_put(buf);
  186. return 0;
  187. }
  188. /**
  189. * tty_audit_buf_get - Get an audit buffer.
  190. *
  191. * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
  192. * if TTY auditing is disabled or out of memory. Otherwise, return a new
  193. * reference to the buffer.
  194. */
  195. static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty,
  196. unsigned icanon)
  197. {
  198. struct tty_audit_buf *buf, *buf2;
  199. unsigned long flags;
  200. buf = NULL;
  201. buf2 = NULL;
  202. spin_lock_irqsave(&current->sighand->siglock, flags);
  203. if (likely(!current->signal->audit_tty))
  204. goto out;
  205. buf = current->signal->tty_audit_buf;
  206. if (buf) {
  207. atomic_inc(&buf->count);
  208. goto out;
  209. }
  210. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  211. buf2 = tty_audit_buf_alloc(tty->driver->major,
  212. tty->driver->minor_start + tty->index,
  213. icanon);
  214. if (buf2 == NULL) {
  215. audit_log_lost("out of memory in TTY auditing");
  216. return NULL;
  217. }
  218. spin_lock_irqsave(&current->sighand->siglock, flags);
  219. if (!current->signal->audit_tty)
  220. goto out;
  221. buf = current->signal->tty_audit_buf;
  222. if (!buf) {
  223. current->signal->tty_audit_buf = buf2;
  224. buf = buf2;
  225. buf2 = NULL;
  226. }
  227. atomic_inc(&buf->count);
  228. /* Fall through */
  229. out:
  230. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  231. if (buf2)
  232. tty_audit_buf_free(buf2);
  233. return buf;
  234. }
  235. /**
  236. * tty_audit_add_data - Add data for TTY auditing.
  237. *
  238. * Audit @data of @size from @tty, if necessary.
  239. */
  240. void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  241. size_t size, unsigned icanon)
  242. {
  243. struct tty_audit_buf *buf;
  244. int major, minor;
  245. if (unlikely(size == 0))
  246. return;
  247. if (tty->driver->type == TTY_DRIVER_TYPE_PTY
  248. && tty->driver->subtype == PTY_TYPE_MASTER)
  249. return;
  250. buf = tty_audit_buf_get(tty, icanon);
  251. if (!buf)
  252. return;
  253. mutex_lock(&buf->mutex);
  254. major = tty->driver->major;
  255. minor = tty->driver->minor_start + tty->index;
  256. if (buf->major != major || buf->minor != minor
  257. || buf->icanon != icanon) {
  258. tty_audit_buf_push(buf);
  259. buf->major = major;
  260. buf->minor = minor;
  261. buf->icanon = icanon;
  262. }
  263. do {
  264. size_t run;
  265. run = N_TTY_BUF_SIZE - buf->valid;
  266. if (run > size)
  267. run = size;
  268. memcpy(buf->data + buf->valid, data, run);
  269. buf->valid += run;
  270. data += run;
  271. size -= run;
  272. if (buf->valid == N_TTY_BUF_SIZE)
  273. tty_audit_buf_push(buf);
  274. } while (size != 0);
  275. mutex_unlock(&buf->mutex);
  276. tty_audit_buf_put(buf);
  277. }
  278. /**
  279. * tty_audit_push - Push buffered data out
  280. *
  281. * Make sure no audit data is pending for @tty on the current process.
  282. */
  283. void tty_audit_push(struct tty_struct *tty)
  284. {
  285. struct tty_audit_buf *buf;
  286. unsigned long flags;
  287. spin_lock_irqsave(&current->sighand->siglock, flags);
  288. if (likely(!current->signal->audit_tty)) {
  289. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  290. return;
  291. }
  292. buf = current->signal->tty_audit_buf;
  293. if (buf)
  294. atomic_inc(&buf->count);
  295. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  296. if (buf) {
  297. int major, minor;
  298. major = tty->driver->major;
  299. minor = tty->driver->minor_start + tty->index;
  300. mutex_lock(&buf->mutex);
  301. if (buf->major == major && buf->minor == minor)
  302. tty_audit_buf_push(buf);
  303. mutex_unlock(&buf->mutex);
  304. tty_audit_buf_put(buf);
  305. }
  306. }