auditsc.c 30 KB

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