tty_audit.c 8.6 KB

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