auditsc.c 31 KB

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