tty_audit.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. pid_t pid = task_pid_nr(tsk);
  61. uid_t uid = from_kuid(&init_user_ns, task_uid(tsk));
  62. uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(tsk));
  63. unsigned int sessionid = audit_get_sessionid(tsk);
  64. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  65. if (ab) {
  66. char name[sizeof(tsk->comm)];
  67. audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
  68. " minor=%d comm=", description, pid, uid,
  69. loginuid, sessionid, major, minor);
  70. get_task_comm(name, tsk);
  71. audit_log_untrustedstring(ab, name);
  72. audit_log_format(ab, " data=");
  73. audit_log_n_hex(ab, data, size);
  74. audit_log_end(ab);
  75. }
  76. }
  77. /**
  78. * tty_audit_buf_push - Push buffered data out
  79. *
  80. * Generate an audit message from the contents of @buf, which is owned by
  81. * the current task. @buf->mutex must be locked.
  82. */
  83. static void tty_audit_buf_push(struct tty_audit_buf *buf)
  84. {
  85. if (buf->valid == 0)
  86. return;
  87. if (audit_enabled == 0) {
  88. buf->valid = 0;
  89. return;
  90. }
  91. tty_audit_log("tty", buf->major, buf->minor, buf->data, buf->valid);
  92. buf->valid = 0;
  93. }
  94. /**
  95. * tty_audit_exit - Handle a task exit
  96. *
  97. * Make sure all buffered data is written out and deallocate the buffer.
  98. * Only needs to be called if current->signal->tty_audit_buf != %NULL.
  99. */
  100. void tty_audit_exit(void)
  101. {
  102. struct tty_audit_buf *buf;
  103. buf = current->signal->tty_audit_buf;
  104. current->signal->tty_audit_buf = NULL;
  105. if (!buf)
  106. return;
  107. mutex_lock(&buf->mutex);
  108. tty_audit_buf_push(buf);
  109. mutex_unlock(&buf->mutex);
  110. tty_audit_buf_put(buf);
  111. }
  112. /**
  113. * tty_audit_fork - Copy TTY audit state for a new task
  114. *
  115. * Set up TTY audit state in @sig from current. @sig needs no locking.
  116. */
  117. void tty_audit_fork(struct signal_struct *sig)
  118. {
  119. sig->audit_tty = current->signal->audit_tty;
  120. sig->audit_tty_log_passwd = current->signal->audit_tty_log_passwd;
  121. }
  122. /**
  123. * tty_audit_tiocsti - Log TIOCSTI
  124. */
  125. void tty_audit_tiocsti(struct tty_struct *tty, char ch)
  126. {
  127. struct tty_audit_buf *buf;
  128. int major, minor, should_audit;
  129. unsigned long flags;
  130. spin_lock_irqsave(&current->sighand->siglock, flags);
  131. should_audit = current->signal->audit_tty;
  132. buf = current->signal->tty_audit_buf;
  133. if (buf)
  134. atomic_inc(&buf->count);
  135. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  136. major = tty->driver->major;
  137. minor = tty->driver->minor_start + tty->index;
  138. if (buf) {
  139. mutex_lock(&buf->mutex);
  140. if (buf->major == major && buf->minor == minor)
  141. tty_audit_buf_push(buf);
  142. mutex_unlock(&buf->mutex);
  143. tty_audit_buf_put(buf);
  144. }
  145. if (should_audit && audit_enabled) {
  146. kuid_t auid;
  147. unsigned int sessionid;
  148. auid = audit_get_loginuid(current);
  149. sessionid = audit_get_sessionid(current);
  150. tty_audit_log("ioctl=TIOCSTI", major, minor, &ch, 1);
  151. }
  152. }
  153. /**
  154. * tty_audit_push_current - Flush current's pending audit data
  155. *
  156. * Try to lock sighand and get a reference to the tty audit buffer if available.
  157. * Flush the buffer or return an appropriate error code.
  158. */
  159. int tty_audit_push_current(void)
  160. {
  161. struct tty_audit_buf *buf = ERR_PTR(-EPERM);
  162. struct task_struct *tsk = current;
  163. unsigned long flags;
  164. if (!lock_task_sighand(tsk, &flags))
  165. return -ESRCH;
  166. if (tsk->signal->audit_tty) {
  167. buf = tsk->signal->tty_audit_buf;
  168. if (buf)
  169. atomic_inc(&buf->count);
  170. }
  171. unlock_task_sighand(tsk, &flags);
  172. /*
  173. * Return 0 when signal->audit_tty set
  174. * but tsk->signal->tty_audit_buf == NULL.
  175. */
  176. if (!buf || IS_ERR(buf))
  177. return PTR_ERR(buf);
  178. mutex_lock(&buf->mutex);
  179. tty_audit_buf_push(buf);
  180. mutex_unlock(&buf->mutex);
  181. tty_audit_buf_put(buf);
  182. return 0;
  183. }
  184. /**
  185. * tty_audit_buf_get - Get an audit buffer.
  186. *
  187. * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
  188. * if TTY auditing is disabled or out of memory. Otherwise, return a new
  189. * reference to the buffer.
  190. */
  191. static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty,
  192. unsigned icanon)
  193. {
  194. struct tty_audit_buf *buf, *buf2;
  195. unsigned long flags;
  196. buf = NULL;
  197. buf2 = NULL;
  198. spin_lock_irqsave(&current->sighand->siglock, flags);
  199. if (likely(!current->signal->audit_tty))
  200. goto out;
  201. buf = current->signal->tty_audit_buf;
  202. if (buf) {
  203. atomic_inc(&buf->count);
  204. goto out;
  205. }
  206. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  207. buf2 = tty_audit_buf_alloc(tty->driver->major,
  208. tty->driver->minor_start + tty->index,
  209. icanon);
  210. if (buf2 == NULL) {
  211. audit_log_lost("out of memory in TTY auditing");
  212. return NULL;
  213. }
  214. spin_lock_irqsave(&current->sighand->siglock, flags);
  215. if (!current->signal->audit_tty)
  216. goto out;
  217. buf = current->signal->tty_audit_buf;
  218. if (!buf) {
  219. current->signal->tty_audit_buf = buf2;
  220. buf = buf2;
  221. buf2 = NULL;
  222. }
  223. atomic_inc(&buf->count);
  224. /* Fall through */
  225. out:
  226. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  227. if (buf2)
  228. tty_audit_buf_free(buf2);
  229. return buf;
  230. }
  231. /**
  232. * tty_audit_add_data - Add data for TTY auditing.
  233. *
  234. * Audit @data of @size from @tty, if necessary.
  235. */
  236. void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  237. size_t size, unsigned icanon)
  238. {
  239. struct tty_audit_buf *buf;
  240. int major, minor;
  241. int audit_log_tty_passwd;
  242. unsigned long flags;
  243. if (unlikely(size == 0))
  244. return;
  245. spin_lock_irqsave(&current->sighand->siglock, flags);
  246. audit_log_tty_passwd = current->signal->audit_tty_log_passwd;
  247. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  248. if (!audit_log_tty_passwd && icanon && !L_ECHO(tty))
  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, icanon);
  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 != icanon) {
  261. tty_audit_buf_push(buf);
  262. buf->major = major;
  263. buf->minor = minor;
  264. buf->icanon = 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(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. unsigned long flags;
  290. spin_lock_irqsave(&current->sighand->siglock, flags);
  291. if (likely(!current->signal->audit_tty)) {
  292. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  293. return;
  294. }
  295. buf = current->signal->tty_audit_buf;
  296. if (buf)
  297. atomic_inc(&buf->count);
  298. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  299. if (buf) {
  300. int major, minor;
  301. major = tty->driver->major;
  302. minor = tty->driver->minor_start + tty->index;
  303. mutex_lock(&buf->mutex);
  304. if (buf->major == major && buf->minor == minor)
  305. tty_audit_buf_push(buf);
  306. mutex_unlock(&buf->mutex);
  307. tty_audit_buf_put(buf);
  308. }
  309. }