auditsc.c 33 KB

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