auditsc.c 33 KB

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