auditsc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /* auditsc.c -- System-call auditing support
  2. * Handles all system-call specific auditing features.
  3. *
  4. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  5. * All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  22. *
  23. * Many of the ideas implemented here are from Stephen C. Tweedie,
  24. * especially the idea of avoiding a copy by using getname.
  25. *
  26. * The method for actual interception of syscall entry and exit (not in
  27. * this file -- see entry.S) is based on a GPL'd patch written by
  28. * okir@suse.de and Copyright 2003 SuSE Linux AG.
  29. *
  30. */
  31. #include <linux/init.h>
  32. #include <asm/atomic.h>
  33. #include <asm/types.h>
  34. #include <linux/mm.h>
  35. #include <linux/module.h>
  36. #include <linux/mount.h>
  37. #include <linux/socket.h>
  38. #include <linux/audit.h>
  39. #include <linux/personality.h>
  40. #include <linux/time.h>
  41. #include <linux/kthread.h>
  42. #include <linux/netlink.h>
  43. #include <asm/unistd.h>
  44. /* 0 = no checking
  45. 1 = put_count checking
  46. 2 = verbose put_count checking
  47. */
  48. #define AUDIT_DEBUG 0
  49. /* No syscall auditing will take place unless audit_enabled != 0. */
  50. extern int audit_enabled;
  51. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  52. * for saving names from getname(). */
  53. #define AUDIT_NAMES 20
  54. /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
  55. * audit_context from being used for nameless inodes from
  56. * path_lookup. */
  57. #define AUDIT_NAMES_RESERVED 7
  58. /* At task start time, the audit_state is set in the audit_context using
  59. a per-task filter. At syscall entry, the audit_state is augmented by
  60. the syscall filter. */
  61. enum audit_state {
  62. AUDIT_DISABLED, /* Do not create per-task audit_context.
  63. * No syscall-specific audit records can
  64. * be generated. */
  65. AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
  66. * but don't necessarily fill it in at
  67. * syscall entry time (i.e., filter
  68. * instead). */
  69. AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
  70. * and always fill it in at syscall
  71. * entry time. This makes a full
  72. * syscall record available if some
  73. * other part of the kernel decides it
  74. * should be recorded. */
  75. AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
  76. * always fill it in at syscall entry
  77. * time, and always write out the audit
  78. * record at syscall exit time. */
  79. };
  80. /* When fs/namei.c:getname() is called, we store the pointer in name and
  81. * we don't let putname() free it (instead we free all of the saved
  82. * pointers at syscall exit time).
  83. *
  84. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  85. struct audit_names {
  86. const char *name;
  87. unsigned long ino;
  88. dev_t dev;
  89. umode_t mode;
  90. uid_t uid;
  91. gid_t gid;
  92. dev_t rdev;
  93. unsigned flags;
  94. };
  95. struct audit_aux_data {
  96. struct audit_aux_data *next;
  97. int type;
  98. };
  99. #define AUDIT_AUX_IPCPERM 0
  100. struct audit_aux_data_ipcctl {
  101. struct audit_aux_data d;
  102. struct ipc_perm p;
  103. unsigned long qbytes;
  104. uid_t uid;
  105. gid_t gid;
  106. mode_t mode;
  107. };
  108. struct audit_aux_data_socketcall {
  109. struct audit_aux_data d;
  110. int nargs;
  111. unsigned long args[0];
  112. };
  113. struct audit_aux_data_sockaddr {
  114. struct audit_aux_data d;
  115. int len;
  116. char a[0];
  117. };
  118. struct audit_aux_data_path {
  119. struct audit_aux_data d;
  120. struct dentry *dentry;
  121. struct vfsmount *mnt;
  122. };
  123. /* The per-task audit context. */
  124. struct audit_context {
  125. int in_syscall; /* 1 if task is in a syscall */
  126. enum audit_state state;
  127. unsigned int serial; /* serial number for record */
  128. struct timespec ctime; /* time of syscall entry */
  129. uid_t loginuid; /* login uid (identity) */
  130. int major; /* syscall number */
  131. unsigned long argv[4]; /* syscall arguments */
  132. int return_valid; /* return code is valid */
  133. long return_code;/* syscall return code */
  134. int auditable; /* 1 if record should be written */
  135. int name_count;
  136. struct audit_names names[AUDIT_NAMES];
  137. struct dentry * pwd;
  138. struct vfsmount * pwdmnt;
  139. struct audit_context *previous; /* For nested syscalls */
  140. struct audit_aux_data *aux;
  141. /* Save things to print about task_struct */
  142. pid_t pid;
  143. uid_t uid, euid, suid, fsuid;
  144. gid_t gid, egid, sgid, fsgid;
  145. unsigned long personality;
  146. int arch;
  147. #if AUDIT_DEBUG
  148. int put_count;
  149. int ino_count;
  150. #endif
  151. };
  152. /* Public API */
  153. /* There are three lists of rules -- one to search at task creation
  154. * time, one to search at syscall entry time, and another to search at
  155. * syscall exit time. */
  156. static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  157. LIST_HEAD_INIT(audit_filter_list[0]),
  158. LIST_HEAD_INIT(audit_filter_list[1]),
  159. LIST_HEAD_INIT(audit_filter_list[2]),
  160. LIST_HEAD_INIT(audit_filter_list[3]),
  161. LIST_HEAD_INIT(audit_filter_list[4]),
  162. #if AUDIT_NR_FILTERS != 5
  163. #error Fix audit_filter_list initialiser
  164. #endif
  165. };
  166. struct audit_entry {
  167. struct list_head list;
  168. struct rcu_head rcu;
  169. struct audit_rule rule;
  170. };
  171. extern int audit_pid;
  172. /* Check to see if two rules are identical. It is called from
  173. * audit_del_rule during AUDIT_DEL. */
  174. static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
  175. {
  176. int i;
  177. if (a->flags != b->flags)
  178. return 1;
  179. if (a->action != b->action)
  180. return 1;
  181. if (a->field_count != b->field_count)
  182. return 1;
  183. for (i = 0; i < a->field_count; i++) {
  184. if (a->fields[i] != b->fields[i]
  185. || a->values[i] != b->values[i])
  186. return 1;
  187. }
  188. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  189. if (a->mask[i] != b->mask[i])
  190. return 1;
  191. return 0;
  192. }
  193. /* Note that audit_add_rule and audit_del_rule are called via
  194. * audit_receive() in audit.c, and are protected by
  195. * audit_netlink_sem. */
  196. static inline void audit_add_rule(struct audit_entry *entry,
  197. struct list_head *list)
  198. {
  199. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  200. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  201. list_add_rcu(&entry->list, list);
  202. } else {
  203. list_add_tail_rcu(&entry->list, list);
  204. }
  205. }
  206. static void audit_free_rule(struct rcu_head *head)
  207. {
  208. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  209. kfree(e);
  210. }
  211. /* Note that audit_add_rule and audit_del_rule are called via
  212. * audit_receive() in audit.c, and are protected by
  213. * audit_netlink_sem. */
  214. static inline int audit_del_rule(struct audit_rule *rule,
  215. struct list_head *list)
  216. {
  217. struct audit_entry *e;
  218. /* Do not use the _rcu iterator here, since this is the only
  219. * deletion routine. */
  220. list_for_each_entry(e, list, list) {
  221. if (!audit_compare_rule(rule, &e->rule)) {
  222. list_del_rcu(&e->list);
  223. call_rcu(&e->rcu, audit_free_rule);
  224. return 0;
  225. }
  226. }
  227. return -ENOENT; /* No matching rule */
  228. }
  229. /* Copy rule from user-space to kernel-space. Called during
  230. * AUDIT_ADD. */
  231. static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
  232. {
  233. int i;
  234. if (s->action != AUDIT_NEVER
  235. && s->action != AUDIT_POSSIBLE
  236. && s->action != AUDIT_ALWAYS)
  237. return -1;
  238. if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
  239. return -1;
  240. if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
  241. return -1;
  242. d->flags = s->flags;
  243. d->action = s->action;
  244. d->field_count = s->field_count;
  245. for (i = 0; i < d->field_count; i++) {
  246. d->fields[i] = s->fields[i];
  247. d->values[i] = s->values[i];
  248. }
  249. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
  250. return 0;
  251. }
  252. static int audit_list_rules(void *_dest)
  253. {
  254. int pid, seq;
  255. int *dest = _dest;
  256. struct audit_entry *entry;
  257. int i;
  258. pid = dest[0];
  259. seq = dest[1];
  260. kfree(dest);
  261. down(&audit_netlink_sem);
  262. /* The *_rcu iterators not needed here because we are
  263. always called with audit_netlink_sem held. */
  264. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  265. list_for_each_entry(entry, &audit_filter_list[i], list)
  266. audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
  267. &entry->rule, sizeof(entry->rule));
  268. }
  269. audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  270. up(&audit_netlink_sem);
  271. return 0;
  272. }
  273. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  274. uid_t loginuid)
  275. {
  276. struct audit_entry *entry;
  277. struct task_struct *tsk;
  278. int *dest;
  279. int err = 0;
  280. unsigned listnr;
  281. switch (type) {
  282. case AUDIT_LIST:
  283. /* We can't just spew out the rules here because we might fill
  284. * the available socket buffer space and deadlock waiting for
  285. * auditctl to read from it... which isn't ever going to
  286. * happen if we're actually running in the context of auditctl
  287. * trying to _send_ the stuff */
  288. dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
  289. if (!dest)
  290. return -ENOMEM;
  291. dest[0] = pid;
  292. dest[1] = seq;
  293. tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
  294. if (IS_ERR(tsk)) {
  295. kfree(dest);
  296. err = PTR_ERR(tsk);
  297. }
  298. break;
  299. case AUDIT_ADD:
  300. if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
  301. return -ENOMEM;
  302. if (audit_copy_rule(&entry->rule, data)) {
  303. kfree(entry);
  304. return -EINVAL;
  305. }
  306. listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
  307. audit_add_rule(entry, &audit_filter_list[listnr]);
  308. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  309. "auid=%u added an audit rule\n", loginuid);
  310. break;
  311. case AUDIT_DEL:
  312. listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
  313. if (listnr >= AUDIT_NR_FILTERS)
  314. return -EINVAL;
  315. err = audit_del_rule(data, &audit_filter_list[listnr]);
  316. if (!err)
  317. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  318. "auid=%u removed an audit rule\n", loginuid);
  319. break;
  320. default:
  321. return -EINVAL;
  322. }
  323. return err;
  324. }
  325. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  326. * otherwise. */
  327. static int audit_filter_rules(struct task_struct *tsk,
  328. struct audit_rule *rule,
  329. struct audit_context *ctx,
  330. enum audit_state *state)
  331. {
  332. int i, j;
  333. for (i = 0; i < rule->field_count; i++) {
  334. u32 field = rule->fields[i] & ~AUDIT_NEGATE;
  335. u32 value = rule->values[i];
  336. int result = 0;
  337. switch (field) {
  338. case AUDIT_PID:
  339. result = (tsk->pid == value);
  340. break;
  341. case AUDIT_UID:
  342. result = (tsk->uid == value);
  343. break;
  344. case AUDIT_EUID:
  345. result = (tsk->euid == value);
  346. break;
  347. case AUDIT_SUID:
  348. result = (tsk->suid == value);
  349. break;
  350. case AUDIT_FSUID:
  351. result = (tsk->fsuid == value);
  352. break;
  353. case AUDIT_GID:
  354. result = (tsk->gid == value);
  355. break;
  356. case AUDIT_EGID:
  357. result = (tsk->egid == value);
  358. break;
  359. case AUDIT_SGID:
  360. result = (tsk->sgid == value);
  361. break;
  362. case AUDIT_FSGID:
  363. result = (tsk->fsgid == value);
  364. break;
  365. case AUDIT_PERS:
  366. result = (tsk->personality == value);
  367. break;
  368. case AUDIT_ARCH:
  369. if (ctx)
  370. result = (ctx->arch == value);
  371. break;
  372. case AUDIT_EXIT:
  373. if (ctx && ctx->return_valid)
  374. result = (ctx->return_code == value);
  375. break;
  376. case AUDIT_SUCCESS:
  377. if (ctx && ctx->return_valid)
  378. result = (ctx->return_valid == AUDITSC_SUCCESS);
  379. break;
  380. case AUDIT_DEVMAJOR:
  381. if (ctx) {
  382. for (j = 0; j < ctx->name_count; j++) {
  383. if (MAJOR(ctx->names[j].dev)==value) {
  384. ++result;
  385. break;
  386. }
  387. }
  388. }
  389. break;
  390. case AUDIT_DEVMINOR:
  391. if (ctx) {
  392. for (j = 0; j < ctx->name_count; j++) {
  393. if (MINOR(ctx->names[j].dev)==value) {
  394. ++result;
  395. break;
  396. }
  397. }
  398. }
  399. break;
  400. case AUDIT_INODE:
  401. if (ctx) {
  402. for (j = 0; j < ctx->name_count; j++) {
  403. if (ctx->names[j].ino == value) {
  404. ++result;
  405. break;
  406. }
  407. }
  408. }
  409. break;
  410. case AUDIT_LOGINUID:
  411. result = 0;
  412. if (ctx)
  413. result = (ctx->loginuid == value);
  414. break;
  415. case AUDIT_ARG0:
  416. case AUDIT_ARG1:
  417. case AUDIT_ARG2:
  418. case AUDIT_ARG3:
  419. if (ctx)
  420. result = (ctx->argv[field-AUDIT_ARG0]==value);
  421. break;
  422. }
  423. if (rule->fields[i] & AUDIT_NEGATE)
  424. result = !result;
  425. if (!result)
  426. return 0;
  427. }
  428. switch (rule->action) {
  429. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  430. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  431. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  432. }
  433. return 1;
  434. }
  435. /* At process creation time, we can determine if system-call auditing is
  436. * completely disabled for this task. Since we only have the task
  437. * structure at this point, we can only check uid and gid.
  438. */
  439. static enum audit_state audit_filter_task(struct task_struct *tsk)
  440. {
  441. struct audit_entry *e;
  442. enum audit_state state;
  443. rcu_read_lock();
  444. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  445. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  446. rcu_read_unlock();
  447. return state;
  448. }
  449. }
  450. rcu_read_unlock();
  451. return AUDIT_BUILD_CONTEXT;
  452. }
  453. /* At syscall entry and exit time, this filter is called if the
  454. * audit_state is not low enough that auditing cannot take place, but is
  455. * also not high enough that we already know we have to write an audit
  456. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  457. */
  458. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  459. struct audit_context *ctx,
  460. struct list_head *list)
  461. {
  462. struct audit_entry *e;
  463. enum audit_state state;
  464. int word = AUDIT_WORD(ctx->major);
  465. int bit = AUDIT_BIT(ctx->major);
  466. if (audit_pid && tsk->pid == audit_pid)
  467. return AUDIT_DISABLED;
  468. rcu_read_lock();
  469. list_for_each_entry_rcu(e, list, list) {
  470. if ((e->rule.mask[word] & bit) == bit
  471. && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
  472. rcu_read_unlock();
  473. return state;
  474. }
  475. }
  476. rcu_read_unlock();
  477. return AUDIT_BUILD_CONTEXT;
  478. }
  479. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  480. struct audit_rule *rule,
  481. enum audit_state *state)
  482. {
  483. int i;
  484. for (i = 0; i < rule->field_count; i++) {
  485. u32 field = rule->fields[i] & ~AUDIT_NEGATE;
  486. u32 value = rule->values[i];
  487. int result = 0;
  488. switch (field) {
  489. case AUDIT_PID:
  490. result = (cb->creds.pid == value);
  491. break;
  492. case AUDIT_UID:
  493. result = (cb->creds.uid == value);
  494. break;
  495. case AUDIT_GID:
  496. result = (cb->creds.gid == value);
  497. break;
  498. case AUDIT_LOGINUID:
  499. result = (cb->loginuid == value);
  500. break;
  501. }
  502. if (rule->fields[i] & AUDIT_NEGATE)
  503. result = !result;
  504. if (!result)
  505. return 0;
  506. }
  507. switch (rule->action) {
  508. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  509. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  510. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  511. }
  512. return 1;
  513. }
  514. int audit_filter_user(struct netlink_skb_parms *cb, int type)
  515. {
  516. struct audit_entry *e;
  517. enum audit_state state;
  518. int ret = 1;
  519. rcu_read_lock();
  520. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  521. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  522. if (state == AUDIT_DISABLED)
  523. ret = 0;
  524. break;
  525. }
  526. }
  527. rcu_read_unlock();
  528. return ret; /* Audit by default */
  529. }
  530. /* This should be called with task_lock() held. */
  531. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  532. int return_valid,
  533. int return_code)
  534. {
  535. struct audit_context *context = tsk->audit_context;
  536. if (likely(!context))
  537. return NULL;
  538. context->return_valid = return_valid;
  539. context->return_code = return_code;
  540. if (context->in_syscall && !context->auditable) {
  541. enum audit_state state;
  542. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  543. if (state == AUDIT_RECORD_CONTEXT)
  544. context->auditable = 1;
  545. }
  546. context->pid = tsk->pid;
  547. context->uid = tsk->uid;
  548. context->gid = tsk->gid;
  549. context->euid = tsk->euid;
  550. context->suid = tsk->suid;
  551. context->fsuid = tsk->fsuid;
  552. context->egid = tsk->egid;
  553. context->sgid = tsk->sgid;
  554. context->fsgid = tsk->fsgid;
  555. context->personality = tsk->personality;
  556. tsk->audit_context = NULL;
  557. return context;
  558. }
  559. static inline void audit_free_names(struct audit_context *context)
  560. {
  561. int i;
  562. #if AUDIT_DEBUG == 2
  563. if (context->auditable
  564. ||context->put_count + context->ino_count != context->name_count) {
  565. printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
  566. " name_count=%d put_count=%d"
  567. " ino_count=%d [NOT freeing]\n",
  568. __LINE__,
  569. context->serial, context->major, context->in_syscall,
  570. context->name_count, context->put_count,
  571. context->ino_count);
  572. for (i = 0; i < context->name_count; i++)
  573. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  574. context->names[i].name,
  575. context->names[i].name);
  576. dump_stack();
  577. return;
  578. }
  579. #endif
  580. #if AUDIT_DEBUG
  581. context->put_count = 0;
  582. context->ino_count = 0;
  583. #endif
  584. for (i = 0; i < context->name_count; i++)
  585. if (context->names[i].name)
  586. __putname(context->names[i].name);
  587. context->name_count = 0;
  588. if (context->pwd)
  589. dput(context->pwd);
  590. if (context->pwdmnt)
  591. mntput(context->pwdmnt);
  592. context->pwd = NULL;
  593. context->pwdmnt = NULL;
  594. }
  595. static inline void audit_free_aux(struct audit_context *context)
  596. {
  597. struct audit_aux_data *aux;
  598. while ((aux = context->aux)) {
  599. if (aux->type == AUDIT_AVC_PATH) {
  600. struct audit_aux_data_path *axi = (void *)aux;
  601. dput(axi->dentry);
  602. mntput(axi->mnt);
  603. }
  604. context->aux = aux->next;
  605. kfree(aux);
  606. }
  607. }
  608. static inline void audit_zero_context(struct audit_context *context,
  609. enum audit_state state)
  610. {
  611. uid_t loginuid = context->loginuid;
  612. memset(context, 0, sizeof(*context));
  613. context->state = state;
  614. context->loginuid = loginuid;
  615. }
  616. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  617. {
  618. struct audit_context *context;
  619. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  620. return NULL;
  621. audit_zero_context(context, state);
  622. return context;
  623. }
  624. /* Filter on the task information and allocate a per-task audit context
  625. * if necessary. Doing so turns on system call auditing for the
  626. * specified task. This is called from copy_process, so no lock is
  627. * needed. */
  628. int audit_alloc(struct task_struct *tsk)
  629. {
  630. struct audit_context *context;
  631. enum audit_state state;
  632. if (likely(!audit_enabled))
  633. return 0; /* Return if not auditing. */
  634. state = audit_filter_task(tsk);
  635. if (likely(state == AUDIT_DISABLED))
  636. return 0;
  637. if (!(context = audit_alloc_context(state))) {
  638. audit_log_lost("out of memory in audit_alloc");
  639. return -ENOMEM;
  640. }
  641. /* Preserve login uid */
  642. context->loginuid = -1;
  643. if (current->audit_context)
  644. context->loginuid = current->audit_context->loginuid;
  645. tsk->audit_context = context;
  646. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  647. return 0;
  648. }
  649. static inline void audit_free_context(struct audit_context *context)
  650. {
  651. struct audit_context *previous;
  652. int count = 0;
  653. do {
  654. previous = context->previous;
  655. if (previous || (count && count < 10)) {
  656. ++count;
  657. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  658. " freeing multiple contexts (%d)\n",
  659. context->serial, context->major,
  660. context->name_count, count);
  661. }
  662. audit_free_names(context);
  663. audit_free_aux(context);
  664. kfree(context);
  665. context = previous;
  666. } while (context);
  667. if (count >= 10)
  668. printk(KERN_ERR "audit: freed %d contexts\n", count);
  669. }
  670. static void audit_log_task_info(struct audit_buffer *ab)
  671. {
  672. char name[sizeof(current->comm)];
  673. struct mm_struct *mm = current->mm;
  674. struct vm_area_struct *vma;
  675. get_task_comm(name, current);
  676. audit_log_format(ab, " comm=");
  677. audit_log_untrustedstring(ab, name);
  678. if (!mm)
  679. return;
  680. down_read(&mm->mmap_sem);
  681. vma = mm->mmap;
  682. while (vma) {
  683. if ((vma->vm_flags & VM_EXECUTABLE) &&
  684. vma->vm_file) {
  685. audit_log_d_path(ab, "exe=",
  686. vma->vm_file->f_dentry,
  687. vma->vm_file->f_vfsmnt);
  688. break;
  689. }
  690. vma = vma->vm_next;
  691. }
  692. up_read(&mm->mmap_sem);
  693. }
  694. static void audit_log_exit(struct audit_context *context)
  695. {
  696. int i;
  697. struct audit_buffer *ab;
  698. struct audit_aux_data *aux;
  699. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  700. if (!ab)
  701. return; /* audit_panic has been called */
  702. audit_log_format(ab, "arch=%x syscall=%d",
  703. context->arch, context->major);
  704. if (context->personality != PER_LINUX)
  705. audit_log_format(ab, " per=%lx", context->personality);
  706. if (context->return_valid)
  707. audit_log_format(ab, " success=%s exit=%ld",
  708. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  709. context->return_code);
  710. audit_log_format(ab,
  711. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  712. " pid=%d auid=%u uid=%u gid=%u"
  713. " euid=%u suid=%u fsuid=%u"
  714. " egid=%u sgid=%u fsgid=%u",
  715. context->argv[0],
  716. context->argv[1],
  717. context->argv[2],
  718. context->argv[3],
  719. context->name_count,
  720. context->pid,
  721. context->loginuid,
  722. context->uid,
  723. context->gid,
  724. context->euid, context->suid, context->fsuid,
  725. context->egid, context->sgid, context->fsgid);
  726. audit_log_task_info(ab);
  727. audit_log_end(ab);
  728. for (aux = context->aux; aux; aux = aux->next) {
  729. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  730. if (!ab)
  731. continue; /* audit_panic has been called */
  732. switch (aux->type) {
  733. case AUDIT_IPC: {
  734. struct audit_aux_data_ipcctl *axi = (void *)aux;
  735. audit_log_format(ab,
  736. " qbytes=%lx iuid=%u igid=%u mode=%x",
  737. axi->qbytes, axi->uid, axi->gid, axi->mode);
  738. break; }
  739. case AUDIT_SOCKETCALL: {
  740. int i;
  741. struct audit_aux_data_socketcall *axs = (void *)aux;
  742. audit_log_format(ab, "nargs=%d", axs->nargs);
  743. for (i=0; i<axs->nargs; i++)
  744. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  745. break; }
  746. case AUDIT_SOCKADDR: {
  747. struct audit_aux_data_sockaddr *axs = (void *)aux;
  748. audit_log_format(ab, "saddr=");
  749. audit_log_hex(ab, axs->a, axs->len);
  750. break; }
  751. case AUDIT_AVC_PATH: {
  752. struct audit_aux_data_path *axi = (void *)aux;
  753. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  754. break; }
  755. }
  756. audit_log_end(ab);
  757. }
  758. if (context->pwd && context->pwdmnt) {
  759. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  760. if (ab) {
  761. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  762. audit_log_end(ab);
  763. }
  764. }
  765. for (i = 0; i < context->name_count; i++) {
  766. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  767. if (!ab)
  768. continue; /* audit_panic has been called */
  769. audit_log_format(ab, "item=%d", i);
  770. if (context->names[i].name) {
  771. audit_log_format(ab, " name=");
  772. audit_log_untrustedstring(ab, context->names[i].name);
  773. }
  774. audit_log_format(ab, " flags=%x\n", context->names[i].flags);
  775. if (context->names[i].ino != (unsigned long)-1)
  776. audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
  777. " ouid=%u ogid=%u rdev=%02x:%02x",
  778. context->names[i].ino,
  779. MAJOR(context->names[i].dev),
  780. MINOR(context->names[i].dev),
  781. context->names[i].mode,
  782. context->names[i].uid,
  783. context->names[i].gid,
  784. MAJOR(context->names[i].rdev),
  785. MINOR(context->names[i].rdev));
  786. audit_log_end(ab);
  787. }
  788. }
  789. /* Free a per-task audit context. Called from copy_process and
  790. * __put_task_struct. */
  791. void audit_free(struct task_struct *tsk)
  792. {
  793. struct audit_context *context;
  794. task_lock(tsk);
  795. context = audit_get_context(tsk, 0, 0);
  796. task_unlock(tsk);
  797. if (likely(!context))
  798. return;
  799. /* Check for system calls that do not go through the exit
  800. * function (e.g., exit_group), then free context block. */
  801. if (context->in_syscall && context->auditable)
  802. audit_log_exit(context);
  803. audit_free_context(context);
  804. }
  805. /* Fill in audit context at syscall entry. This only happens if the
  806. * audit context was created when the task was created and the state or
  807. * filters demand the audit context be built. If the state from the
  808. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  809. * then the record will be written at syscall exit time (otherwise, it
  810. * will only be written if another part of the kernel requests that it
  811. * be written). */
  812. void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
  813. unsigned long a1, unsigned long a2,
  814. unsigned long a3, unsigned long a4)
  815. {
  816. struct audit_context *context = tsk->audit_context;
  817. enum audit_state state;
  818. BUG_ON(!context);
  819. /* This happens only on certain architectures that make system
  820. * calls in kernel_thread via the entry.S interface, instead of
  821. * with direct calls. (If you are porting to a new
  822. * architecture, hitting this condition can indicate that you
  823. * got the _exit/_leave calls backward in entry.S.)
  824. *
  825. * i386 no
  826. * x86_64 no
  827. * ppc64 yes (see arch/ppc64/kernel/misc.S)
  828. *
  829. * This also happens with vm86 emulation in a non-nested manner
  830. * (entries without exits), so this case must be caught.
  831. */
  832. if (context->in_syscall) {
  833. struct audit_context *newctx;
  834. #if defined(__NR_vm86) && defined(__NR_vm86old)
  835. /* vm86 mode should only be entered once */
  836. if (major == __NR_vm86 || major == __NR_vm86old)
  837. return;
  838. #endif
  839. #if AUDIT_DEBUG
  840. printk(KERN_ERR
  841. "audit(:%d) pid=%d in syscall=%d;"
  842. " entering syscall=%d\n",
  843. context->serial, tsk->pid, context->major, major);
  844. #endif
  845. newctx = audit_alloc_context(context->state);
  846. if (newctx) {
  847. newctx->previous = context;
  848. context = newctx;
  849. tsk->audit_context = newctx;
  850. } else {
  851. /* If we can't alloc a new context, the best we
  852. * can do is to leak memory (any pending putname
  853. * will be lost). The only other alternative is
  854. * to abandon auditing. */
  855. audit_zero_context(context, context->state);
  856. }
  857. }
  858. BUG_ON(context->in_syscall || context->name_count);
  859. if (!audit_enabled)
  860. return;
  861. context->arch = arch;
  862. context->major = major;
  863. context->argv[0] = a1;
  864. context->argv[1] = a2;
  865. context->argv[2] = a3;
  866. context->argv[3] = a4;
  867. state = context->state;
  868. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  869. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  870. if (likely(state == AUDIT_DISABLED))
  871. return;
  872. context->serial = audit_serial();
  873. context->ctime = CURRENT_TIME;
  874. context->in_syscall = 1;
  875. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  876. }
  877. /* Tear down after system call. If the audit context has been marked as
  878. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  879. * filtering, or because some other part of the kernel write an audit
  880. * message), then write out the syscall information. In call cases,
  881. * free the names stored from getname(). */
  882. void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
  883. {
  884. struct audit_context *context;
  885. get_task_struct(tsk);
  886. task_lock(tsk);
  887. context = audit_get_context(tsk, valid, return_code);
  888. task_unlock(tsk);
  889. /* Not having a context here is ok, since the parent may have
  890. * called __put_task_struct. */
  891. if (likely(!context))
  892. return;
  893. if (context->in_syscall && context->auditable)
  894. audit_log_exit(context);
  895. context->in_syscall = 0;
  896. context->auditable = 0;
  897. if (context->previous) {
  898. struct audit_context *new_context = context->previous;
  899. context->previous = NULL;
  900. audit_free_context(context);
  901. tsk->audit_context = new_context;
  902. } else {
  903. audit_free_names(context);
  904. audit_free_aux(context);
  905. audit_zero_context(context, context->state);
  906. tsk->audit_context = context;
  907. }
  908. put_task_struct(tsk);
  909. }
  910. /* Add a name to the list. Called from fs/namei.c:getname(). */
  911. void audit_getname(const char *name)
  912. {
  913. struct audit_context *context = current->audit_context;
  914. if (!context || IS_ERR(name) || !name)
  915. return;
  916. if (!context->in_syscall) {
  917. #if AUDIT_DEBUG == 2
  918. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  919. __FILE__, __LINE__, context->serial, name);
  920. dump_stack();
  921. #endif
  922. return;
  923. }
  924. BUG_ON(context->name_count >= AUDIT_NAMES);
  925. context->names[context->name_count].name = name;
  926. context->names[context->name_count].ino = (unsigned long)-1;
  927. ++context->name_count;
  928. if (!context->pwd) {
  929. read_lock(&current->fs->lock);
  930. context->pwd = dget(current->fs->pwd);
  931. context->pwdmnt = mntget(current->fs->pwdmnt);
  932. read_unlock(&current->fs->lock);
  933. }
  934. }
  935. /* Intercept a putname request. Called from
  936. * include/linux/fs.h:putname(). If we have stored the name from
  937. * getname in the audit context, then we delay the putname until syscall
  938. * exit. */
  939. void audit_putname(const char *name)
  940. {
  941. struct audit_context *context = current->audit_context;
  942. BUG_ON(!context);
  943. if (!context->in_syscall) {
  944. #if AUDIT_DEBUG == 2
  945. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  946. __FILE__, __LINE__, context->serial, name);
  947. if (context->name_count) {
  948. int i;
  949. for (i = 0; i < context->name_count; i++)
  950. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  951. context->names[i].name,
  952. context->names[i].name);
  953. }
  954. #endif
  955. __putname(name);
  956. }
  957. #if AUDIT_DEBUG
  958. else {
  959. ++context->put_count;
  960. if (context->put_count > context->name_count) {
  961. printk(KERN_ERR "%s:%d(:%d): major=%d"
  962. " in_syscall=%d putname(%p) name_count=%d"
  963. " put_count=%d\n",
  964. __FILE__, __LINE__,
  965. context->serial, context->major,
  966. context->in_syscall, name, context->name_count,
  967. context->put_count);
  968. dump_stack();
  969. }
  970. }
  971. #endif
  972. }
  973. /* Store the inode and device from a lookup. Called from
  974. * fs/namei.c:path_lookup(). */
  975. void audit_inode(const char *name, const struct inode *inode, unsigned flags)
  976. {
  977. int idx;
  978. struct audit_context *context = current->audit_context;
  979. if (!context->in_syscall)
  980. return;
  981. if (context->name_count
  982. && context->names[context->name_count-1].name
  983. && context->names[context->name_count-1].name == name)
  984. idx = context->name_count - 1;
  985. else if (context->name_count > 1
  986. && context->names[context->name_count-2].name
  987. && context->names[context->name_count-2].name == name)
  988. idx = context->name_count - 2;
  989. else {
  990. /* FIXME: how much do we care about inodes that have no
  991. * associated name? */
  992. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  993. return;
  994. idx = context->name_count++;
  995. context->names[idx].name = NULL;
  996. #if AUDIT_DEBUG
  997. ++context->ino_count;
  998. #endif
  999. }
  1000. context->names[idx].flags = flags;
  1001. context->names[idx].ino = inode->i_ino;
  1002. context->names[idx].dev = inode->i_sb->s_dev;
  1003. context->names[idx].mode = inode->i_mode;
  1004. context->names[idx].uid = inode->i_uid;
  1005. context->names[idx].gid = inode->i_gid;
  1006. context->names[idx].rdev = inode->i_rdev;
  1007. }
  1008. void auditsc_get_stamp(struct audit_context *ctx,
  1009. struct timespec *t, unsigned int *serial)
  1010. {
  1011. t->tv_sec = ctx->ctime.tv_sec;
  1012. t->tv_nsec = ctx->ctime.tv_nsec;
  1013. *serial = ctx->serial;
  1014. ctx->auditable = 1;
  1015. }
  1016. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1017. {
  1018. if (task->audit_context) {
  1019. struct audit_buffer *ab;
  1020. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1021. if (ab) {
  1022. audit_log_format(ab, "login pid=%d uid=%u "
  1023. "old auid=%u new auid=%u",
  1024. task->pid, task->uid,
  1025. task->audit_context->loginuid, loginuid);
  1026. audit_log_end(ab);
  1027. }
  1028. task->audit_context->loginuid = loginuid;
  1029. }
  1030. return 0;
  1031. }
  1032. uid_t audit_get_loginuid(struct audit_context *ctx)
  1033. {
  1034. return ctx ? ctx->loginuid : -1;
  1035. }
  1036. int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1037. {
  1038. struct audit_aux_data_ipcctl *ax;
  1039. struct audit_context *context = current->audit_context;
  1040. if (likely(!context))
  1041. return 0;
  1042. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1043. if (!ax)
  1044. return -ENOMEM;
  1045. ax->qbytes = qbytes;
  1046. ax->uid = uid;
  1047. ax->gid = gid;
  1048. ax->mode = mode;
  1049. ax->d.type = AUDIT_IPC;
  1050. ax->d.next = context->aux;
  1051. context->aux = (void *)ax;
  1052. return 0;
  1053. }
  1054. int audit_socketcall(int nargs, unsigned long *args)
  1055. {
  1056. struct audit_aux_data_socketcall *ax;
  1057. struct audit_context *context = current->audit_context;
  1058. if (likely(!context))
  1059. return 0;
  1060. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1061. if (!ax)
  1062. return -ENOMEM;
  1063. ax->nargs = nargs;
  1064. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1065. ax->d.type = AUDIT_SOCKETCALL;
  1066. ax->d.next = context->aux;
  1067. context->aux = (void *)ax;
  1068. return 0;
  1069. }
  1070. int audit_sockaddr(int len, void *a)
  1071. {
  1072. struct audit_aux_data_sockaddr *ax;
  1073. struct audit_context *context = current->audit_context;
  1074. if (likely(!context))
  1075. return 0;
  1076. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1077. if (!ax)
  1078. return -ENOMEM;
  1079. ax->len = len;
  1080. memcpy(ax->a, a, len);
  1081. ax->d.type = AUDIT_SOCKADDR;
  1082. ax->d.next = context->aux;
  1083. context->aux = (void *)ax;
  1084. return 0;
  1085. }
  1086. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1087. {
  1088. struct audit_aux_data_path *ax;
  1089. struct audit_context *context = current->audit_context;
  1090. if (likely(!context))
  1091. return 0;
  1092. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1093. if (!ax)
  1094. return -ENOMEM;
  1095. ax->dentry = dget(dentry);
  1096. ax->mnt = mntget(mnt);
  1097. ax->d.type = AUDIT_AVC_PATH;
  1098. ax->d.next = context->aux;
  1099. context->aux = (void *)ax;
  1100. return 0;
  1101. }
  1102. void audit_signal_info(int sig, struct task_struct *t)
  1103. {
  1104. extern pid_t audit_sig_pid;
  1105. extern uid_t audit_sig_uid;
  1106. if (unlikely(audit_pid && t->pid == audit_pid)) {
  1107. if (sig == SIGTERM || sig == SIGHUP) {
  1108. struct audit_context *ctx = current->audit_context;
  1109. audit_sig_pid = current->pid;
  1110. if (ctx)
  1111. audit_sig_uid = ctx->loginuid;
  1112. else
  1113. audit_sig_uid = current->uid;
  1114. }
  1115. }
  1116. }