tty_audit.c 8.3 KB

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