tty_audit.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/file.h>
  13. #include <linux/fdtable.h>
  14. #include <linux/tty.h>
  15. struct tty_audit_buf {
  16. atomic_t count;
  17. struct mutex mutex; /* Protects all data below */
  18. int major, minor; /* The TTY which the data is from */
  19. unsigned icanon:1;
  20. size_t valid;
  21. unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
  22. };
  23. static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
  24. int icanon)
  25. {
  26. struct tty_audit_buf *buf;
  27. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  28. if (!buf)
  29. goto err;
  30. if (PAGE_SIZE != N_TTY_BUF_SIZE)
  31. buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  32. else
  33. buf->data = (unsigned char *)__get_free_page(GFP_KERNEL);
  34. if (!buf->data)
  35. goto err_buf;
  36. atomic_set(&buf->count, 1);
  37. mutex_init(&buf->mutex);
  38. buf->major = major;
  39. buf->minor = minor;
  40. buf->icanon = icanon;
  41. buf->valid = 0;
  42. return buf;
  43. err_buf:
  44. kfree(buf);
  45. err:
  46. return NULL;
  47. }
  48. static void tty_audit_buf_free(struct tty_audit_buf *buf)
  49. {
  50. WARN_ON(buf->valid != 0);
  51. if (PAGE_SIZE != N_TTY_BUF_SIZE)
  52. kfree(buf->data);
  53. else
  54. free_page((unsigned long)buf->data);
  55. kfree(buf);
  56. }
  57. static void tty_audit_buf_put(struct tty_audit_buf *buf)
  58. {
  59. if (atomic_dec_and_test(&buf->count))
  60. tty_audit_buf_free(buf);
  61. }
  62. static void tty_audit_log(const char *description, struct task_struct *tsk,
  63. uid_t loginuid, unsigned sessionid, int major,
  64. int minor, unsigned char *data, size_t size)
  65. {
  66. struct audit_buffer *ab;
  67. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  68. if (ab) {
  69. char name[sizeof(tsk->comm)];
  70. uid_t uid = task_uid(tsk);
  71. audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
  72. "major=%d minor=%d comm=", description,
  73. tsk->pid, uid, loginuid, sessionid,
  74. major, minor);
  75. get_task_comm(name, tsk);
  76. audit_log_untrustedstring(ab, name);
  77. audit_log_format(ab, " data=");
  78. audit_log_n_hex(ab, data, size);
  79. audit_log_end(ab);
  80. }
  81. }
  82. /**
  83. * tty_audit_buf_push - Push buffered data out
  84. *
  85. * Generate an audit message from the contents of @buf, which is owned by
  86. * @tsk with @loginuid. @buf->mutex must be locked.
  87. */
  88. static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
  89. unsigned int sessionid,
  90. struct tty_audit_buf *buf)
  91. {
  92. if (buf->valid == 0)
  93. return;
  94. if (audit_enabled == 0)
  95. return;
  96. tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor,
  97. buf->data, buf->valid);
  98. buf->valid = 0;
  99. }
  100. /**
  101. * tty_audit_buf_push_current - Push buffered data out
  102. *
  103. * Generate an audit message from the contents of @buf, which is owned by
  104. * the current task. @buf->mutex must be locked.
  105. */
  106. static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
  107. {
  108. uid_t auid = audit_get_loginuid(current);
  109. unsigned int sessionid = audit_get_sessionid(current);
  110. tty_audit_buf_push(current, auid, sessionid, buf);
  111. }
  112. /**
  113. * tty_audit_exit - Handle a task exit
  114. *
  115. * Make sure all buffered data is written out and deallocate the buffer.
  116. * Only needs to be called if current->signal->tty_audit_buf != %NULL.
  117. */
  118. void tty_audit_exit(void)
  119. {
  120. struct tty_audit_buf *buf;
  121. spin_lock_irq(&current->sighand->siglock);
  122. buf = current->signal->tty_audit_buf;
  123. current->signal->tty_audit_buf = NULL;
  124. spin_unlock_irq(&current->sighand->siglock);
  125. if (!buf)
  126. return;
  127. mutex_lock(&buf->mutex);
  128. tty_audit_buf_push_current(buf);
  129. mutex_unlock(&buf->mutex);
  130. tty_audit_buf_put(buf);
  131. }
  132. /**
  133. * tty_audit_fork - Copy TTY audit state for a new task
  134. *
  135. * Set up TTY audit state in @sig from current. @sig needs no locking.
  136. */
  137. void tty_audit_fork(struct signal_struct *sig)
  138. {
  139. spin_lock_irq(&current->sighand->siglock);
  140. sig->audit_tty = current->signal->audit_tty;
  141. spin_unlock_irq(&current->sighand->siglock);
  142. sig->tty_audit_buf = NULL;
  143. }
  144. /**
  145. * tty_audit_tiocsti - Log TIOCSTI
  146. */
  147. void tty_audit_tiocsti(struct tty_struct *tty, char ch)
  148. {
  149. struct tty_audit_buf *buf;
  150. int major, minor, should_audit;
  151. spin_lock_irq(&current->sighand->siglock);
  152. should_audit = current->signal->audit_tty;
  153. buf = current->signal->tty_audit_buf;
  154. if (buf)
  155. atomic_inc(&buf->count);
  156. spin_unlock_irq(&current->sighand->siglock);
  157. major = tty->driver->major;
  158. minor = tty->driver->minor_start + tty->index;
  159. if (buf) {
  160. mutex_lock(&buf->mutex);
  161. if (buf->major == major && buf->minor == minor)
  162. tty_audit_buf_push_current(buf);
  163. mutex_unlock(&buf->mutex);
  164. tty_audit_buf_put(buf);
  165. }
  166. if (should_audit && audit_enabled) {
  167. uid_t auid;
  168. unsigned int sessionid;
  169. auid = audit_get_loginuid(current);
  170. sessionid = audit_get_sessionid(current);
  171. tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
  172. minor, &ch, 1);
  173. }
  174. }
  175. /**
  176. * tty_audit_push_task - Flush task's pending audit data
  177. */
  178. void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
  179. {
  180. struct tty_audit_buf *buf;
  181. spin_lock_irq(&tsk->sighand->siglock);
  182. buf = tsk->signal->tty_audit_buf;
  183. if (buf)
  184. atomic_inc(&buf->count);
  185. spin_unlock_irq(&tsk->sighand->siglock);
  186. if (!buf)
  187. return;
  188. mutex_lock(&buf->mutex);
  189. tty_audit_buf_push(tsk, loginuid, sessionid, buf);
  190. mutex_unlock(&buf->mutex);
  191. tty_audit_buf_put(buf);
  192. }
  193. /**
  194. * tty_audit_buf_get - Get an audit buffer.
  195. *
  196. * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
  197. * if TTY auditing is disabled or out of memory. Otherwise, return a new
  198. * reference to the buffer.
  199. */
  200. static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
  201. {
  202. struct tty_audit_buf *buf, *buf2;
  203. buf = NULL;
  204. buf2 = NULL;
  205. spin_lock_irq(&current->sighand->siglock);
  206. if (likely(!current->signal->audit_tty))
  207. goto out;
  208. buf = current->signal->tty_audit_buf;
  209. if (buf) {
  210. atomic_inc(&buf->count);
  211. goto out;
  212. }
  213. spin_unlock_irq(&current->sighand->siglock);
  214. buf2 = tty_audit_buf_alloc(tty->driver->major,
  215. tty->driver->minor_start + tty->index,
  216. tty->icanon);
  217. if (buf2 == NULL) {
  218. audit_log_lost("out of memory in TTY auditing");
  219. return NULL;
  220. }
  221. spin_lock_irq(&current->sighand->siglock);
  222. if (!current->signal->audit_tty)
  223. goto out;
  224. buf = current->signal->tty_audit_buf;
  225. if (!buf) {
  226. current->signal->tty_audit_buf = buf2;
  227. buf = buf2;
  228. buf2 = NULL;
  229. }
  230. atomic_inc(&buf->count);
  231. /* Fall through */
  232. out:
  233. spin_unlock_irq(&current->sighand->siglock);
  234. if (buf2)
  235. tty_audit_buf_free(buf2);
  236. return buf;
  237. }
  238. /**
  239. * tty_audit_add_data - Add data for TTY auditing.
  240. *
  241. * Audit @data of @size from @tty, if necessary.
  242. */
  243. void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  244. size_t size)
  245. {
  246. struct tty_audit_buf *buf;
  247. int major, minor;
  248. if (unlikely(size == 0))
  249. return;
  250. if (tty->driver->type == TTY_DRIVER_TYPE_PTY
  251. && tty->driver->subtype == PTY_TYPE_MASTER)
  252. return;
  253. buf = tty_audit_buf_get(tty);
  254. if (!buf)
  255. return;
  256. mutex_lock(&buf->mutex);
  257. major = tty->driver->major;
  258. minor = tty->driver->minor_start + tty->index;
  259. if (buf->major != major || buf->minor != minor
  260. || buf->icanon != tty->icanon) {
  261. tty_audit_buf_push_current(buf);
  262. buf->major = major;
  263. buf->minor = minor;
  264. buf->icanon = tty->icanon;
  265. }
  266. do {
  267. size_t run;
  268. run = N_TTY_BUF_SIZE - buf->valid;
  269. if (run > size)
  270. run = size;
  271. memcpy(buf->data + buf->valid, data, run);
  272. buf->valid += run;
  273. data += run;
  274. size -= run;
  275. if (buf->valid == N_TTY_BUF_SIZE)
  276. tty_audit_buf_push_current(buf);
  277. } while (size != 0);
  278. mutex_unlock(&buf->mutex);
  279. tty_audit_buf_put(buf);
  280. }
  281. /**
  282. * tty_audit_push - Push buffered data out
  283. *
  284. * Make sure no audit data is pending for @tty on the current process.
  285. */
  286. void tty_audit_push(struct tty_struct *tty)
  287. {
  288. struct tty_audit_buf *buf;
  289. spin_lock_irq(&current->sighand->siglock);
  290. if (likely(!current->signal->audit_tty)) {
  291. spin_unlock_irq(&current->sighand->siglock);
  292. return;
  293. }
  294. buf = current->signal->tty_audit_buf;
  295. if (buf)
  296. atomic_inc(&buf->count);
  297. spin_unlock_irq(&current->sighand->siglock);
  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_current(buf);
  305. mutex_unlock(&buf->mutex);
  306. tty_audit_buf_put(buf);
  307. }
  308. }