auditsc.c 30 KB

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