tty_audit.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. struct mutex mutex; /* Protects all data below */
  16. dev_t dev; /* 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_ref(void)
  22. {
  23. struct tty_audit_buf *buf;
  24. buf = current->signal->tty_audit_buf;
  25. WARN_ON(buf == ERR_PTR(-ESRCH));
  26. return buf;
  27. }
  28. static struct tty_audit_buf *tty_audit_buf_alloc(void)
  29. {
  30. struct tty_audit_buf *buf;
  31. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  32. if (!buf)
  33. goto err;
  34. buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  35. if (!buf->data)
  36. goto err_buf;
  37. mutex_init(&buf->mutex);
  38. buf->dev = MKDEV(0, 0);
  39. buf->icanon = 0;
  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. kfree(buf->data);
  51. kfree(buf);
  52. }
  53. static void tty_audit_log(const char *description, dev_t dev,
  54. unsigned char *data, size_t size)
  55. {
  56. struct audit_buffer *ab;
  57. struct task_struct *tsk = current;
  58. pid_t pid = task_pid_nr(tsk);
  59. uid_t uid = from_kuid(&init_user_ns, task_uid(tsk));
  60. uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(tsk));
  61. unsigned int sessionid = audit_get_sessionid(tsk);
  62. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  63. if (ab) {
  64. char name[sizeof(tsk->comm)];
  65. audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
  66. " minor=%d comm=", description, pid, uid,
  67. loginuid, sessionid, MAJOR(dev), MINOR(dev));
  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. * the current task. @buf->mutex must be locked.
  80. */
  81. static void tty_audit_buf_push(struct tty_audit_buf *buf)
  82. {
  83. if (buf->valid == 0)
  84. return;
  85. if (audit_enabled == 0) {
  86. buf->valid = 0;
  87. return;
  88. }
  89. tty_audit_log("tty", buf->dev, buf->data, buf->valid);
  90. buf->valid = 0;
  91. }
  92. /**
  93. * tty_audit_exit - Handle a task exit
  94. *
  95. * Make sure all buffered data is written out and deallocate the buffer.
  96. * Only needs to be called if current->signal->tty_audit_buf != %NULL.
  97. *
  98. * The process is single-threaded at this point; no other threads share
  99. * current->signal.
  100. */
  101. void tty_audit_exit(void)
  102. {
  103. struct tty_audit_buf *buf;
  104. buf = xchg(&current->signal->tty_audit_buf, ERR_PTR(-ESRCH));
  105. if (!buf)
  106. return;
  107. tty_audit_buf_push(buf);
  108. tty_audit_buf_free(buf);
  109. }
  110. /**
  111. * tty_audit_fork - Copy TTY audit state for a new task
  112. *
  113. * Set up TTY audit state in @sig from current. @sig needs no locking.
  114. */
  115. void tty_audit_fork(struct signal_struct *sig)
  116. {
  117. sig->audit_tty = current->signal->audit_tty;
  118. }
  119. /**
  120. * tty_audit_tiocsti - Log TIOCSTI
  121. */
  122. void tty_audit_tiocsti(struct tty_struct *tty, char ch)
  123. {
  124. dev_t dev;
  125. dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
  126. if (tty_audit_push())
  127. return;
  128. if (audit_enabled)
  129. tty_audit_log("ioctl=TIOCSTI", dev, &ch, 1);
  130. }
  131. /**
  132. * tty_audit_push - Flush current's pending audit data
  133. *
  134. * Returns 0 if success, -EPERM if tty audit is disabled
  135. */
  136. int tty_audit_push(void)
  137. {
  138. struct tty_audit_buf *buf;
  139. if (~current->signal->audit_tty & AUDIT_TTY_ENABLE)
  140. return -EPERM;
  141. buf = tty_audit_buf_ref();
  142. if (!IS_ERR_OR_NULL(buf)) {
  143. mutex_lock(&buf->mutex);
  144. tty_audit_buf_push(buf);
  145. mutex_unlock(&buf->mutex);
  146. }
  147. return 0;
  148. }
  149. /**
  150. * tty_audit_buf_get - Get an audit buffer.
  151. *
  152. * Get an audit buffer, allocate it if necessary. Return %NULL
  153. * if out of memory or ERR_PTR(-ESRCH) if tty_audit_exit() has already
  154. * occurred. Otherwise, return a new reference to the buffer.
  155. */
  156. static struct tty_audit_buf *tty_audit_buf_get(void)
  157. {
  158. struct tty_audit_buf *buf;
  159. buf = tty_audit_buf_ref();
  160. if (buf)
  161. return buf;
  162. buf = tty_audit_buf_alloc();
  163. if (buf == NULL) {
  164. audit_log_lost("out of memory in TTY auditing");
  165. return NULL;
  166. }
  167. /* Race to use this buffer, free it if another wins */
  168. if (cmpxchg(&current->signal->tty_audit_buf, NULL, buf) != NULL)
  169. tty_audit_buf_free(buf);
  170. return tty_audit_buf_ref();
  171. }
  172. /**
  173. * tty_audit_add_data - Add data for TTY auditing.
  174. *
  175. * Audit @data of @size from @tty, if necessary.
  176. */
  177. void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
  178. {
  179. struct tty_audit_buf *buf;
  180. unsigned int icanon = !!L_ICANON(tty);
  181. unsigned int audit_tty;
  182. dev_t dev;
  183. audit_tty = READ_ONCE(current->signal->audit_tty);
  184. if (~audit_tty & AUDIT_TTY_ENABLE)
  185. return;
  186. if (unlikely(size == 0))
  187. return;
  188. if (tty->driver->type == TTY_DRIVER_TYPE_PTY
  189. && tty->driver->subtype == PTY_TYPE_MASTER)
  190. return;
  191. if ((~audit_tty & AUDIT_TTY_LOG_PASSWD) && icanon && !L_ECHO(tty))
  192. return;
  193. buf = tty_audit_buf_get();
  194. if (IS_ERR_OR_NULL(buf))
  195. return;
  196. mutex_lock(&buf->mutex);
  197. dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
  198. if (buf->dev != dev || buf->icanon != icanon) {
  199. tty_audit_buf_push(buf);
  200. buf->dev = dev;
  201. buf->icanon = icanon;
  202. }
  203. do {
  204. size_t run;
  205. run = N_TTY_BUF_SIZE - buf->valid;
  206. if (run > size)
  207. run = size;
  208. memcpy(buf->data + buf->valid, data, run);
  209. buf->valid += run;
  210. data += run;
  211. size -= run;
  212. if (buf->valid == N_TTY_BUF_SIZE)
  213. tty_audit_buf_push(buf);
  214. } while (size != 0);
  215. mutex_unlock(&buf->mutex);
  216. }