tty_audit.c 5.6 KB

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