tty_audit.c 5.5 KB

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