auditsc.c 28 KB

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