auditsc.c 29 KB

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