auditsc.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  6. * Copyright (C) 2005 IBM Corporation
  7. * All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  24. *
  25. * Many of the ideas implemented here are from Stephen C. Tweedie,
  26. * especially the idea of avoiding a copy by using getname.
  27. *
  28. * The method for actual interception of syscall entry and exit (not in
  29. * this file -- see entry.S) is based on a GPL'd patch written by
  30. * okir@suse.de and Copyright 2003 SuSE Linux AG.
  31. *
  32. * The support of additional filter rules compares (>, <, >=, <=) was
  33. * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
  34. *
  35. * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
  36. * filesystem information.
  37. *
  38. * Subject and object context labeling support added by <danjones@us.ibm.com>
  39. * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
  40. */
  41. #include <linux/init.h>
  42. #include <asm/types.h>
  43. #include <asm/atomic.h>
  44. #include <asm/types.h>
  45. #include <linux/fs.h>
  46. #include <linux/namei.h>
  47. #include <linux/mm.h>
  48. #include <linux/module.h>
  49. #include <linux/mount.h>
  50. #include <linux/socket.h>
  51. #include <linux/audit.h>
  52. #include <linux/personality.h>
  53. #include <linux/time.h>
  54. #include <linux/kthread.h>
  55. #include <linux/netlink.h>
  56. #include <linux/compiler.h>
  57. #include <asm/unistd.h>
  58. #include <linux/security.h>
  59. /* 0 = no checking
  60. 1 = put_count checking
  61. 2 = verbose put_count checking
  62. */
  63. #define AUDIT_DEBUG 0
  64. /* No syscall auditing will take place unless audit_enabled != 0. */
  65. extern int audit_enabled;
  66. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  67. * for saving names from getname(). */
  68. #define AUDIT_NAMES 20
  69. /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
  70. * audit_context from being used for nameless inodes from
  71. * path_lookup. */
  72. #define AUDIT_NAMES_RESERVED 7
  73. /* At task start time, the audit_state is set in the audit_context using
  74. a per-task filter. At syscall entry, the audit_state is augmented by
  75. the syscall filter. */
  76. enum audit_state {
  77. AUDIT_DISABLED, /* Do not create per-task audit_context.
  78. * No syscall-specific audit records can
  79. * be generated. */
  80. AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
  81. * but don't necessarily fill it in at
  82. * syscall entry time (i.e., filter
  83. * instead). */
  84. AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
  85. * and always fill it in at syscall
  86. * entry time. This makes a full
  87. * syscall record available if some
  88. * other part of the kernel decides it
  89. * should be recorded. */
  90. AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
  91. * always fill it in at syscall entry
  92. * time, and always write out the audit
  93. * record at syscall exit time. */
  94. };
  95. /* When fs/namei.c:getname() is called, we store the pointer in name and
  96. * we don't let putname() free it (instead we free all of the saved
  97. * pointers at syscall exit time).
  98. *
  99. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  100. struct audit_names {
  101. const char *name;
  102. unsigned long ino;
  103. unsigned long pino;
  104. dev_t dev;
  105. umode_t mode;
  106. uid_t uid;
  107. gid_t gid;
  108. dev_t rdev;
  109. char *ctx;
  110. };
  111. struct audit_aux_data {
  112. struct audit_aux_data *next;
  113. int type;
  114. };
  115. #define AUDIT_AUX_IPCPERM 0
  116. struct audit_aux_data_ipcctl {
  117. struct audit_aux_data d;
  118. struct ipc_perm p;
  119. unsigned long qbytes;
  120. uid_t uid;
  121. gid_t gid;
  122. mode_t mode;
  123. char *ctx;
  124. };
  125. struct audit_aux_data_socketcall {
  126. struct audit_aux_data d;
  127. int nargs;
  128. unsigned long args[0];
  129. };
  130. struct audit_aux_data_sockaddr {
  131. struct audit_aux_data d;
  132. int len;
  133. char a[0];
  134. };
  135. struct audit_aux_data_path {
  136. struct audit_aux_data d;
  137. struct dentry *dentry;
  138. struct vfsmount *mnt;
  139. };
  140. /* The per-task audit context. */
  141. struct audit_context {
  142. int in_syscall; /* 1 if task is in a syscall */
  143. enum audit_state state;
  144. unsigned int serial; /* serial number for record */
  145. struct timespec ctime; /* time of syscall entry */
  146. uid_t loginuid; /* login uid (identity) */
  147. int major; /* syscall number */
  148. unsigned long argv[4]; /* syscall arguments */
  149. int return_valid; /* return code is valid */
  150. long return_code;/* syscall return code */
  151. int auditable; /* 1 if record should be written */
  152. int name_count;
  153. struct audit_names names[AUDIT_NAMES];
  154. struct dentry * pwd;
  155. struct vfsmount * pwdmnt;
  156. struct audit_context *previous; /* For nested syscalls */
  157. struct audit_aux_data *aux;
  158. /* Save things to print about task_struct */
  159. pid_t pid;
  160. uid_t uid, euid, suid, fsuid;
  161. gid_t gid, egid, sgid, fsgid;
  162. unsigned long personality;
  163. int arch;
  164. #if AUDIT_DEBUG
  165. int put_count;
  166. int ino_count;
  167. #endif
  168. };
  169. /* Public API */
  170. /* There are three lists of rules -- one to search at task creation
  171. * time, one to search at syscall entry time, and another to search at
  172. * syscall exit time. */
  173. static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  174. LIST_HEAD_INIT(audit_filter_list[0]),
  175. LIST_HEAD_INIT(audit_filter_list[1]),
  176. LIST_HEAD_INIT(audit_filter_list[2]),
  177. LIST_HEAD_INIT(audit_filter_list[3]),
  178. LIST_HEAD_INIT(audit_filter_list[4]),
  179. LIST_HEAD_INIT(audit_filter_list[5]),
  180. #if AUDIT_NR_FILTERS != 6
  181. #error Fix audit_filter_list initialiser
  182. #endif
  183. };
  184. struct audit_entry {
  185. struct list_head list;
  186. struct rcu_head rcu;
  187. struct audit_rule rule;
  188. };
  189. extern int audit_pid;
  190. /* Copy rule from user-space to kernel-space. Called from
  191. * audit_add_rule during AUDIT_ADD. */
  192. static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
  193. {
  194. int i;
  195. if (s->action != AUDIT_NEVER
  196. && s->action != AUDIT_POSSIBLE
  197. && s->action != AUDIT_ALWAYS)
  198. return -1;
  199. if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
  200. return -1;
  201. if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
  202. return -1;
  203. d->flags = s->flags;
  204. d->action = s->action;
  205. d->field_count = s->field_count;
  206. for (i = 0; i < d->field_count; i++) {
  207. d->fields[i] = s->fields[i];
  208. d->values[i] = s->values[i];
  209. }
  210. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
  211. return 0;
  212. }
  213. /* Check to see if two rules are identical. It is called from
  214. * audit_add_rule during AUDIT_ADD and
  215. * audit_del_rule during AUDIT_DEL. */
  216. static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
  217. {
  218. int i;
  219. if (a->flags != b->flags)
  220. return 1;
  221. if (a->action != b->action)
  222. return 1;
  223. if (a->field_count != b->field_count)
  224. return 1;
  225. for (i = 0; i < a->field_count; i++) {
  226. if (a->fields[i] != b->fields[i]
  227. || a->values[i] != b->values[i])
  228. return 1;
  229. }
  230. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  231. if (a->mask[i] != b->mask[i])
  232. return 1;
  233. return 0;
  234. }
  235. /* Note that audit_add_rule and audit_del_rule are called via
  236. * audit_receive() in audit.c, and are protected by
  237. * audit_netlink_sem. */
  238. static inline int audit_add_rule(struct audit_rule *rule,
  239. struct list_head *list)
  240. {
  241. struct audit_entry *entry;
  242. int i;
  243. /* Do not use the _rcu iterator here, since this is the only
  244. * addition routine. */
  245. list_for_each_entry(entry, list, list) {
  246. if (!audit_compare_rule(rule, &entry->rule)) {
  247. return -EEXIST;
  248. }
  249. }
  250. for (i = 0; i < rule->field_count; i++) {
  251. if (rule->fields[i] & AUDIT_UNUSED_BITS)
  252. return -EINVAL;
  253. if ( rule->fields[i] & AUDIT_NEGATE )
  254. rule->fields[i] |= AUDIT_NOT_EQUAL;
  255. else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 )
  256. rule->fields[i] |= AUDIT_EQUAL;
  257. rule->fields[i] &= (~AUDIT_NEGATE);
  258. }
  259. if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
  260. return -ENOMEM;
  261. if (audit_copy_rule(&entry->rule, rule)) {
  262. kfree(entry);
  263. return -EINVAL;
  264. }
  265. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  266. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  267. list_add_rcu(&entry->list, list);
  268. } else {
  269. list_add_tail_rcu(&entry->list, list);
  270. }
  271. return 0;
  272. }
  273. static inline void audit_free_rule(struct rcu_head *head)
  274. {
  275. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  276. kfree(e);
  277. }
  278. /* Note that audit_add_rule and audit_del_rule are called via
  279. * audit_receive() in audit.c, and are protected by
  280. * audit_netlink_sem. */
  281. static inline int audit_del_rule(struct audit_rule *rule,
  282. struct list_head *list)
  283. {
  284. struct audit_entry *e;
  285. /* Do not use the _rcu iterator here, since this is the only
  286. * deletion routine. */
  287. list_for_each_entry(e, list, list) {
  288. if (!audit_compare_rule(rule, &e->rule)) {
  289. list_del_rcu(&e->list);
  290. call_rcu(&e->rcu, audit_free_rule);
  291. return 0;
  292. }
  293. }
  294. return -ENOENT; /* No matching rule */
  295. }
  296. static int audit_list_rules(void *_dest)
  297. {
  298. int pid, seq;
  299. int *dest = _dest;
  300. struct audit_entry *entry;
  301. int i;
  302. pid = dest[0];
  303. seq = dest[1];
  304. kfree(dest);
  305. down(&audit_netlink_sem);
  306. /* The *_rcu iterators not needed here because we are
  307. always called with audit_netlink_sem held. */
  308. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  309. list_for_each_entry(entry, &audit_filter_list[i], list)
  310. audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
  311. &entry->rule, sizeof(entry->rule));
  312. }
  313. audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  314. up(&audit_netlink_sem);
  315. return 0;
  316. }
  317. /**
  318. * audit_receive_filter - apply all rules to the specified message type
  319. * @type: audit message type
  320. * @pid: target pid for netlink audit messages
  321. * @uid: target uid for netlink audit messages
  322. * @seq: netlink audit message sequence (serial) number
  323. * @data: payload data
  324. * @loginuid: loginuid of sender
  325. */
  326. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  327. uid_t loginuid)
  328. {
  329. struct task_struct *tsk;
  330. int *dest;
  331. int err = 0;
  332. unsigned listnr;
  333. switch (type) {
  334. case AUDIT_LIST:
  335. /* We can't just spew out the rules here because we might fill
  336. * the available socket buffer space and deadlock waiting for
  337. * auditctl to read from it... which isn't ever going to
  338. * happen if we're actually running in the context of auditctl
  339. * trying to _send_ the stuff */
  340. dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
  341. if (!dest)
  342. return -ENOMEM;
  343. dest[0] = pid;
  344. dest[1] = seq;
  345. tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
  346. if (IS_ERR(tsk)) {
  347. kfree(dest);
  348. err = PTR_ERR(tsk);
  349. }
  350. break;
  351. case AUDIT_ADD:
  352. listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
  353. if (listnr >= AUDIT_NR_FILTERS)
  354. return -EINVAL;
  355. err = audit_add_rule(data, &audit_filter_list[listnr]);
  356. if (!err)
  357. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  358. "auid=%u added an audit rule\n", loginuid);
  359. break;
  360. case AUDIT_DEL:
  361. listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
  362. if (listnr >= AUDIT_NR_FILTERS)
  363. return -EINVAL;
  364. err = audit_del_rule(data, &audit_filter_list[listnr]);
  365. if (!err)
  366. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  367. "auid=%u removed an audit rule\n", loginuid);
  368. break;
  369. default:
  370. return -EINVAL;
  371. }
  372. return err;
  373. }
  374. static int audit_comparator(const u32 left, const u32 op, const u32 right)
  375. {
  376. switch (op) {
  377. case AUDIT_EQUAL:
  378. return (left == right);
  379. case AUDIT_NOT_EQUAL:
  380. return (left != right);
  381. case AUDIT_LESS_THAN:
  382. return (left < right);
  383. case AUDIT_LESS_THAN_OR_EQUAL:
  384. return (left <= right);
  385. case AUDIT_GREATER_THAN:
  386. return (left > right);
  387. case AUDIT_GREATER_THAN_OR_EQUAL:
  388. return (left >= right);
  389. default:
  390. return -EINVAL;
  391. }
  392. }
  393. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  394. * otherwise. */
  395. static int audit_filter_rules(struct task_struct *tsk,
  396. struct audit_rule *rule,
  397. struct audit_context *ctx,
  398. enum audit_state *state)
  399. {
  400. int i, j;
  401. for (i = 0; i < rule->field_count; i++) {
  402. u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
  403. u32 op = rule->fields[i] & AUDIT_OPERATORS;
  404. u32 value = rule->values[i];
  405. int result = 0;
  406. switch (field) {
  407. case AUDIT_PID:
  408. result = audit_comparator(tsk->pid, op, value);
  409. break;
  410. case AUDIT_UID:
  411. result = audit_comparator(tsk->uid, op, value);
  412. break;
  413. case AUDIT_EUID:
  414. result = audit_comparator(tsk->euid, op, value);
  415. break;
  416. case AUDIT_SUID:
  417. result = audit_comparator(tsk->suid, op, value);
  418. break;
  419. case AUDIT_FSUID:
  420. result = audit_comparator(tsk->fsuid, op, value);
  421. break;
  422. case AUDIT_GID:
  423. result = audit_comparator(tsk->gid, op, value);
  424. break;
  425. case AUDIT_EGID:
  426. result = audit_comparator(tsk->egid, op, value);
  427. break;
  428. case AUDIT_SGID:
  429. result = audit_comparator(tsk->sgid, op, value);
  430. break;
  431. case AUDIT_FSGID:
  432. result = audit_comparator(tsk->fsgid, op, value);
  433. break;
  434. case AUDIT_PERS:
  435. result = audit_comparator(tsk->personality, op, value);
  436. break;
  437. case AUDIT_ARCH:
  438. if (ctx)
  439. result = audit_comparator(ctx->arch, op, value);
  440. break;
  441. case AUDIT_EXIT:
  442. if (ctx && ctx->return_valid)
  443. result = audit_comparator(ctx->return_code, op, value);
  444. break;
  445. case AUDIT_SUCCESS:
  446. if (ctx && ctx->return_valid) {
  447. if (value)
  448. result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
  449. else
  450. result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
  451. }
  452. break;
  453. case AUDIT_DEVMAJOR:
  454. if (ctx) {
  455. for (j = 0; j < ctx->name_count; j++) {
  456. if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
  457. ++result;
  458. break;
  459. }
  460. }
  461. }
  462. break;
  463. case AUDIT_DEVMINOR:
  464. if (ctx) {
  465. for (j = 0; j < ctx->name_count; j++) {
  466. if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
  467. ++result;
  468. break;
  469. }
  470. }
  471. }
  472. break;
  473. case AUDIT_INODE:
  474. if (ctx) {
  475. for (j = 0; j < ctx->name_count; j++) {
  476. if (audit_comparator(ctx->names[j].ino, op, value) ||
  477. audit_comparator(ctx->names[j].pino, op, value)) {
  478. ++result;
  479. break;
  480. }
  481. }
  482. }
  483. break;
  484. case AUDIT_LOGINUID:
  485. result = 0;
  486. if (ctx)
  487. result = audit_comparator(ctx->loginuid, op, value);
  488. break;
  489. case AUDIT_ARG0:
  490. case AUDIT_ARG1:
  491. case AUDIT_ARG2:
  492. case AUDIT_ARG3:
  493. if (ctx)
  494. result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
  495. break;
  496. }
  497. if (!result)
  498. return 0;
  499. }
  500. switch (rule->action) {
  501. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  502. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  503. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  504. }
  505. return 1;
  506. }
  507. /* At process creation time, we can determine if system-call auditing is
  508. * completely disabled for this task. Since we only have the task
  509. * structure at this point, we can only check uid and gid.
  510. */
  511. static enum audit_state audit_filter_task(struct task_struct *tsk)
  512. {
  513. struct audit_entry *e;
  514. enum audit_state state;
  515. rcu_read_lock();
  516. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  517. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  518. rcu_read_unlock();
  519. return state;
  520. }
  521. }
  522. rcu_read_unlock();
  523. return AUDIT_BUILD_CONTEXT;
  524. }
  525. /* At syscall entry and exit time, this filter is called if the
  526. * audit_state is not low enough that auditing cannot take place, but is
  527. * also not high enough that we already know we have to write an audit
  528. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  529. */
  530. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  531. struct audit_context *ctx,
  532. struct list_head *list)
  533. {
  534. struct audit_entry *e;
  535. enum audit_state state;
  536. if (audit_pid && tsk->tgid == audit_pid)
  537. return AUDIT_DISABLED;
  538. rcu_read_lock();
  539. if (!list_empty(list)) {
  540. int word = AUDIT_WORD(ctx->major);
  541. int bit = AUDIT_BIT(ctx->major);
  542. list_for_each_entry_rcu(e, list, list) {
  543. if ((e->rule.mask[word] & bit) == bit
  544. && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
  545. rcu_read_unlock();
  546. return state;
  547. }
  548. }
  549. }
  550. rcu_read_unlock();
  551. return AUDIT_BUILD_CONTEXT;
  552. }
  553. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  554. struct audit_rule *rule,
  555. enum audit_state *state)
  556. {
  557. int i;
  558. for (i = 0; i < rule->field_count; i++) {
  559. u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
  560. u32 op = rule->fields[i] & AUDIT_OPERATORS;
  561. u32 value = rule->values[i];
  562. int result = 0;
  563. switch (field) {
  564. case AUDIT_PID:
  565. result = audit_comparator(cb->creds.pid, op, value);
  566. break;
  567. case AUDIT_UID:
  568. result = audit_comparator(cb->creds.uid, op, value);
  569. break;
  570. case AUDIT_GID:
  571. result = audit_comparator(cb->creds.gid, op, value);
  572. break;
  573. case AUDIT_LOGINUID:
  574. result = audit_comparator(cb->loginuid, op, value);
  575. break;
  576. }
  577. if (!result)
  578. return 0;
  579. }
  580. switch (rule->action) {
  581. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  582. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  583. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  584. }
  585. return 1;
  586. }
  587. int audit_filter_user(struct netlink_skb_parms *cb, int type)
  588. {
  589. struct audit_entry *e;
  590. enum audit_state state;
  591. int ret = 1;
  592. rcu_read_lock();
  593. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  594. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  595. if (state == AUDIT_DISABLED)
  596. ret = 0;
  597. break;
  598. }
  599. }
  600. rcu_read_unlock();
  601. return ret; /* Audit by default */
  602. }
  603. int audit_filter_type(int type)
  604. {
  605. struct audit_entry *e;
  606. int result = 0;
  607. rcu_read_lock();
  608. if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
  609. goto unlock_and_return;
  610. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
  611. list) {
  612. struct audit_rule *rule = &e->rule;
  613. int i;
  614. for (i = 0; i < rule->field_count; i++) {
  615. u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
  616. u32 op = rule->fields[i] & AUDIT_OPERATORS;
  617. u32 value = rule->values[i];
  618. if ( field == AUDIT_MSGTYPE ) {
  619. result = audit_comparator(type, op, value);
  620. if (!result)
  621. break;
  622. }
  623. }
  624. if (result)
  625. goto unlock_and_return;
  626. }
  627. unlock_and_return:
  628. rcu_read_unlock();
  629. return result;
  630. }
  631. /* This should be called with task_lock() held. */
  632. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  633. int return_valid,
  634. int return_code)
  635. {
  636. struct audit_context *context = tsk->audit_context;
  637. if (likely(!context))
  638. return NULL;
  639. context->return_valid = return_valid;
  640. context->return_code = return_code;
  641. if (context->in_syscall && !context->auditable) {
  642. enum audit_state state;
  643. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  644. if (state == AUDIT_RECORD_CONTEXT)
  645. context->auditable = 1;
  646. }
  647. context->pid = tsk->pid;
  648. context->uid = tsk->uid;
  649. context->gid = tsk->gid;
  650. context->euid = tsk->euid;
  651. context->suid = tsk->suid;
  652. context->fsuid = tsk->fsuid;
  653. context->egid = tsk->egid;
  654. context->sgid = tsk->sgid;
  655. context->fsgid = tsk->fsgid;
  656. context->personality = tsk->personality;
  657. tsk->audit_context = NULL;
  658. return context;
  659. }
  660. static inline void audit_free_names(struct audit_context *context)
  661. {
  662. int i;
  663. #if AUDIT_DEBUG == 2
  664. if (context->auditable
  665. ||context->put_count + context->ino_count != context->name_count) {
  666. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  667. " name_count=%d put_count=%d"
  668. " ino_count=%d [NOT freeing]\n",
  669. __FILE__, __LINE__,
  670. context->serial, context->major, context->in_syscall,
  671. context->name_count, context->put_count,
  672. context->ino_count);
  673. for (i = 0; i < context->name_count; i++) {
  674. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  675. context->names[i].name,
  676. context->names[i].name ?: "(null)");
  677. }
  678. dump_stack();
  679. return;
  680. }
  681. #endif
  682. #if AUDIT_DEBUG
  683. context->put_count = 0;
  684. context->ino_count = 0;
  685. #endif
  686. for (i = 0; i < context->name_count; i++) {
  687. char *p = context->names[i].ctx;
  688. context->names[i].ctx = NULL;
  689. kfree(p);
  690. if (context->names[i].name)
  691. __putname(context->names[i].name);
  692. }
  693. context->name_count = 0;
  694. if (context->pwd)
  695. dput(context->pwd);
  696. if (context->pwdmnt)
  697. mntput(context->pwdmnt);
  698. context->pwd = NULL;
  699. context->pwdmnt = NULL;
  700. }
  701. static inline void audit_free_aux(struct audit_context *context)
  702. {
  703. struct audit_aux_data *aux;
  704. while ((aux = context->aux)) {
  705. if (aux->type == AUDIT_AVC_PATH) {
  706. struct audit_aux_data_path *axi = (void *)aux;
  707. dput(axi->dentry);
  708. mntput(axi->mnt);
  709. }
  710. if ( aux->type == AUDIT_IPC ) {
  711. struct audit_aux_data_ipcctl *axi = (void *)aux;
  712. if (axi->ctx)
  713. kfree(axi->ctx);
  714. }
  715. context->aux = aux->next;
  716. kfree(aux);
  717. }
  718. }
  719. static inline void audit_zero_context(struct audit_context *context,
  720. enum audit_state state)
  721. {
  722. uid_t loginuid = context->loginuid;
  723. memset(context, 0, sizeof(*context));
  724. context->state = state;
  725. context->loginuid = loginuid;
  726. }
  727. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  728. {
  729. struct audit_context *context;
  730. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  731. return NULL;
  732. audit_zero_context(context, state);
  733. return context;
  734. }
  735. /**
  736. * audit_alloc - allocate an audit context block for a task
  737. * @tsk: task
  738. *
  739. * Filter on the task information and allocate a per-task audit context
  740. * if necessary. Doing so turns on system call auditing for the
  741. * specified task. This is called from copy_process, so no lock is
  742. * needed.
  743. */
  744. int audit_alloc(struct task_struct *tsk)
  745. {
  746. struct audit_context *context;
  747. enum audit_state state;
  748. if (likely(!audit_enabled))
  749. return 0; /* Return if not auditing. */
  750. state = audit_filter_task(tsk);
  751. if (likely(state == AUDIT_DISABLED))
  752. return 0;
  753. if (!(context = audit_alloc_context(state))) {
  754. audit_log_lost("out of memory in audit_alloc");
  755. return -ENOMEM;
  756. }
  757. /* Preserve login uid */
  758. context->loginuid = -1;
  759. if (current->audit_context)
  760. context->loginuid = current->audit_context->loginuid;
  761. tsk->audit_context = context;
  762. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  763. return 0;
  764. }
  765. static inline void audit_free_context(struct audit_context *context)
  766. {
  767. struct audit_context *previous;
  768. int count = 0;
  769. do {
  770. previous = context->previous;
  771. if (previous || (count && count < 10)) {
  772. ++count;
  773. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  774. " freeing multiple contexts (%d)\n",
  775. context->serial, context->major,
  776. context->name_count, count);
  777. }
  778. audit_free_names(context);
  779. audit_free_aux(context);
  780. kfree(context);
  781. context = previous;
  782. } while (context);
  783. if (count >= 10)
  784. printk(KERN_ERR "audit: freed %d contexts\n", count);
  785. }
  786. static void audit_log_task_context(struct audit_buffer *ab, gfp_t gfp_mask)
  787. {
  788. char *ctx = NULL;
  789. ssize_t len = 0;
  790. len = security_getprocattr(current, "current", NULL, 0);
  791. if (len < 0) {
  792. if (len != -EINVAL)
  793. goto error_path;
  794. return;
  795. }
  796. ctx = kmalloc(len, gfp_mask);
  797. if (!ctx) {
  798. goto error_path;
  799. return;
  800. }
  801. len = security_getprocattr(current, "current", ctx, len);
  802. if (len < 0 )
  803. goto error_path;
  804. audit_log_format(ab, " subj=%s", ctx);
  805. error_path:
  806. if (ctx)
  807. kfree(ctx);
  808. audit_panic("security_getprocattr error in audit_log_task_context");
  809. return;
  810. }
  811. static void audit_log_task_info(struct audit_buffer *ab, gfp_t gfp_mask)
  812. {
  813. char name[sizeof(current->comm)];
  814. struct mm_struct *mm = current->mm;
  815. struct vm_area_struct *vma;
  816. get_task_comm(name, current);
  817. audit_log_format(ab, " comm=");
  818. audit_log_untrustedstring(ab, name);
  819. if (!mm)
  820. return;
  821. /*
  822. * this is brittle; all callers that pass GFP_ATOMIC will have
  823. * NULL current->mm and we won't get here.
  824. */
  825. down_read(&mm->mmap_sem);
  826. vma = mm->mmap;
  827. while (vma) {
  828. if ((vma->vm_flags & VM_EXECUTABLE) &&
  829. vma->vm_file) {
  830. audit_log_d_path(ab, "exe=",
  831. vma->vm_file->f_dentry,
  832. vma->vm_file->f_vfsmnt);
  833. break;
  834. }
  835. vma = vma->vm_next;
  836. }
  837. up_read(&mm->mmap_sem);
  838. audit_log_task_context(ab, gfp_mask);
  839. }
  840. static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
  841. {
  842. int i;
  843. struct audit_buffer *ab;
  844. struct audit_aux_data *aux;
  845. ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
  846. if (!ab)
  847. return; /* audit_panic has been called */
  848. audit_log_format(ab, "arch=%x syscall=%d",
  849. context->arch, context->major);
  850. if (context->personality != PER_LINUX)
  851. audit_log_format(ab, " per=%lx", context->personality);
  852. if (context->return_valid)
  853. audit_log_format(ab, " success=%s exit=%ld",
  854. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  855. context->return_code);
  856. audit_log_format(ab,
  857. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  858. " pid=%d auid=%u uid=%u gid=%u"
  859. " euid=%u suid=%u fsuid=%u"
  860. " egid=%u sgid=%u fsgid=%u",
  861. context->argv[0],
  862. context->argv[1],
  863. context->argv[2],
  864. context->argv[3],
  865. context->name_count,
  866. context->pid,
  867. context->loginuid,
  868. context->uid,
  869. context->gid,
  870. context->euid, context->suid, context->fsuid,
  871. context->egid, context->sgid, context->fsgid);
  872. audit_log_task_info(ab, gfp_mask);
  873. audit_log_end(ab);
  874. for (aux = context->aux; aux; aux = aux->next) {
  875. ab = audit_log_start(context, gfp_mask, aux->type);
  876. if (!ab)
  877. continue; /* audit_panic has been called */
  878. switch (aux->type) {
  879. case AUDIT_IPC: {
  880. struct audit_aux_data_ipcctl *axi = (void *)aux;
  881. audit_log_format(ab,
  882. " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
  883. axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
  884. break; }
  885. case AUDIT_SOCKETCALL: {
  886. int i;
  887. struct audit_aux_data_socketcall *axs = (void *)aux;
  888. audit_log_format(ab, "nargs=%d", axs->nargs);
  889. for (i=0; i<axs->nargs; i++)
  890. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  891. break; }
  892. case AUDIT_SOCKADDR: {
  893. struct audit_aux_data_sockaddr *axs = (void *)aux;
  894. audit_log_format(ab, "saddr=");
  895. audit_log_hex(ab, axs->a, axs->len);
  896. break; }
  897. case AUDIT_AVC_PATH: {
  898. struct audit_aux_data_path *axi = (void *)aux;
  899. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  900. break; }
  901. }
  902. audit_log_end(ab);
  903. }
  904. if (context->pwd && context->pwdmnt) {
  905. ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
  906. if (ab) {
  907. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  908. audit_log_end(ab);
  909. }
  910. }
  911. for (i = 0; i < context->name_count; i++) {
  912. unsigned long ino = context->names[i].ino;
  913. unsigned long pino = context->names[i].pino;
  914. ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
  915. if (!ab)
  916. continue; /* audit_panic has been called */
  917. audit_log_format(ab, "item=%d", i);
  918. audit_log_format(ab, " name=");
  919. if (context->names[i].name)
  920. audit_log_untrustedstring(ab, context->names[i].name);
  921. else
  922. audit_log_format(ab, "(null)");
  923. if (pino != (unsigned long)-1)
  924. audit_log_format(ab, " parent=%lu", pino);
  925. if (ino != (unsigned long)-1)
  926. audit_log_format(ab, " inode=%lu", ino);
  927. if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
  928. audit_log_format(ab, " dev=%02x:%02x mode=%#o"
  929. " ouid=%u ogid=%u rdev=%02x:%02x",
  930. MAJOR(context->names[i].dev),
  931. MINOR(context->names[i].dev),
  932. context->names[i].mode,
  933. context->names[i].uid,
  934. context->names[i].gid,
  935. MAJOR(context->names[i].rdev),
  936. MINOR(context->names[i].rdev));
  937. if (context->names[i].ctx) {
  938. audit_log_format(ab, " obj=%s",
  939. context->names[i].ctx);
  940. }
  941. audit_log_end(ab);
  942. }
  943. }
  944. /**
  945. * audit_free - free a per-task audit context
  946. * @tsk: task whose audit context block to free
  947. *
  948. * Called from copy_process and __put_task_struct.
  949. */
  950. void audit_free(struct task_struct *tsk)
  951. {
  952. struct audit_context *context;
  953. task_lock(tsk);
  954. context = audit_get_context(tsk, 0, 0);
  955. task_unlock(tsk);
  956. if (likely(!context))
  957. return;
  958. /* Check for system calls that do not go through the exit
  959. * function (e.g., exit_group), then free context block.
  960. * We use GFP_ATOMIC here because we might be doing this
  961. * in the context of the idle thread */
  962. if (context->in_syscall && context->auditable)
  963. audit_log_exit(context, GFP_ATOMIC);
  964. audit_free_context(context);
  965. }
  966. /**
  967. * audit_syscall_entry - fill in an audit record at syscall entry
  968. * @tsk: task being audited
  969. * @arch: architecture type
  970. * @major: major syscall type (function)
  971. * @a1: additional syscall register 1
  972. * @a2: additional syscall register 2
  973. * @a3: additional syscall register 3
  974. * @a4: additional syscall register 4
  975. *
  976. * Fill in audit context at syscall entry. This only happens if the
  977. * audit context was created when the task was created and the state or
  978. * filters demand the audit context be built. If the state from the
  979. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  980. * then the record will be written at syscall exit time (otherwise, it
  981. * will only be written if another part of the kernel requests that it
  982. * be written).
  983. */
  984. void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
  985. unsigned long a1, unsigned long a2,
  986. unsigned long a3, unsigned long a4)
  987. {
  988. struct audit_context *context = tsk->audit_context;
  989. enum audit_state state;
  990. BUG_ON(!context);
  991. /*
  992. * This happens only on certain architectures that make system
  993. * calls in kernel_thread via the entry.S interface, instead of
  994. * with direct calls. (If you are porting to a new
  995. * architecture, hitting this condition can indicate that you
  996. * got the _exit/_leave calls backward in entry.S.)
  997. *
  998. * i386 no
  999. * x86_64 no
  1000. * ppc64 yes (see arch/ppc64/kernel/misc.S)
  1001. *
  1002. * This also happens with vm86 emulation in a non-nested manner
  1003. * (entries without exits), so this case must be caught.
  1004. */
  1005. if (context->in_syscall) {
  1006. struct audit_context *newctx;
  1007. #if AUDIT_DEBUG
  1008. printk(KERN_ERR
  1009. "audit(:%d) pid=%d in syscall=%d;"
  1010. " entering syscall=%d\n",
  1011. context->serial, tsk->pid, context->major, major);
  1012. #endif
  1013. newctx = audit_alloc_context(context->state);
  1014. if (newctx) {
  1015. newctx->previous = context;
  1016. context = newctx;
  1017. tsk->audit_context = newctx;
  1018. } else {
  1019. /* If we can't alloc a new context, the best we
  1020. * can do is to leak memory (any pending putname
  1021. * will be lost). The only other alternative is
  1022. * to abandon auditing. */
  1023. audit_zero_context(context, context->state);
  1024. }
  1025. }
  1026. BUG_ON(context->in_syscall || context->name_count);
  1027. if (!audit_enabled)
  1028. return;
  1029. context->arch = arch;
  1030. context->major = major;
  1031. context->argv[0] = a1;
  1032. context->argv[1] = a2;
  1033. context->argv[2] = a3;
  1034. context->argv[3] = a4;
  1035. state = context->state;
  1036. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  1037. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1038. if (likely(state == AUDIT_DISABLED))
  1039. return;
  1040. context->serial = 0;
  1041. context->ctime = CURRENT_TIME;
  1042. context->in_syscall = 1;
  1043. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  1044. }
  1045. /**
  1046. * audit_syscall_exit - deallocate audit context after a system call
  1047. * @tsk: task being audited
  1048. * @valid: success/failure flag
  1049. * @return_code: syscall return value
  1050. *
  1051. * Tear down after system call. If the audit context has been marked as
  1052. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1053. * filtering, or because some other part of the kernel write an audit
  1054. * message), then write out the syscall information. In call cases,
  1055. * free the names stored from getname().
  1056. */
  1057. void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
  1058. {
  1059. struct audit_context *context;
  1060. get_task_struct(tsk);
  1061. task_lock(tsk);
  1062. context = audit_get_context(tsk, valid, return_code);
  1063. task_unlock(tsk);
  1064. /* Not having a context here is ok, since the parent may have
  1065. * called __put_task_struct. */
  1066. if (likely(!context))
  1067. goto out;
  1068. if (context->in_syscall && context->auditable)
  1069. audit_log_exit(context, GFP_KERNEL);
  1070. context->in_syscall = 0;
  1071. context->auditable = 0;
  1072. if (context->previous) {
  1073. struct audit_context *new_context = context->previous;
  1074. context->previous = NULL;
  1075. audit_free_context(context);
  1076. tsk->audit_context = new_context;
  1077. } else {
  1078. audit_free_names(context);
  1079. audit_free_aux(context);
  1080. tsk->audit_context = context;
  1081. }
  1082. out:
  1083. put_task_struct(tsk);
  1084. }
  1085. /**
  1086. * audit_getname - add a name to the list
  1087. * @name: name to add
  1088. *
  1089. * Add a name to the list of audit names for this context.
  1090. * Called from fs/namei.c:getname().
  1091. */
  1092. void audit_getname(const char *name)
  1093. {
  1094. struct audit_context *context = current->audit_context;
  1095. if (!context || IS_ERR(name) || !name)
  1096. return;
  1097. if (!context->in_syscall) {
  1098. #if AUDIT_DEBUG == 2
  1099. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1100. __FILE__, __LINE__, context->serial, name);
  1101. dump_stack();
  1102. #endif
  1103. return;
  1104. }
  1105. BUG_ON(context->name_count >= AUDIT_NAMES);
  1106. context->names[context->name_count].name = name;
  1107. context->names[context->name_count].ino = (unsigned long)-1;
  1108. ++context->name_count;
  1109. if (!context->pwd) {
  1110. read_lock(&current->fs->lock);
  1111. context->pwd = dget(current->fs->pwd);
  1112. context->pwdmnt = mntget(current->fs->pwdmnt);
  1113. read_unlock(&current->fs->lock);
  1114. }
  1115. }
  1116. /* audit_putname - intercept a putname request
  1117. * @name: name to intercept and delay for putname
  1118. *
  1119. * If we have stored the name from getname in the audit context,
  1120. * then we delay the putname until syscall exit.
  1121. * Called from include/linux/fs.h:putname().
  1122. */
  1123. void audit_putname(const char *name)
  1124. {
  1125. struct audit_context *context = current->audit_context;
  1126. BUG_ON(!context);
  1127. if (!context->in_syscall) {
  1128. #if AUDIT_DEBUG == 2
  1129. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1130. __FILE__, __LINE__, context->serial, name);
  1131. if (context->name_count) {
  1132. int i;
  1133. for (i = 0; i < context->name_count; i++)
  1134. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1135. context->names[i].name,
  1136. context->names[i].name ?: "(null)");
  1137. }
  1138. #endif
  1139. __putname(name);
  1140. }
  1141. #if AUDIT_DEBUG
  1142. else {
  1143. ++context->put_count;
  1144. if (context->put_count > context->name_count) {
  1145. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1146. " in_syscall=%d putname(%p) name_count=%d"
  1147. " put_count=%d\n",
  1148. __FILE__, __LINE__,
  1149. context->serial, context->major,
  1150. context->in_syscall, name, context->name_count,
  1151. context->put_count);
  1152. dump_stack();
  1153. }
  1154. }
  1155. #endif
  1156. }
  1157. void audit_inode_context(int idx, const struct inode *inode)
  1158. {
  1159. struct audit_context *context = current->audit_context;
  1160. char *ctx = NULL;
  1161. int len = 0;
  1162. if (!security_inode_xattr_getsuffix())
  1163. return;
  1164. len = security_inode_getsecurity(inode, (char *)security_inode_xattr_getsuffix(), NULL, 0, 0);
  1165. if (len < 0)
  1166. goto error_path;
  1167. ctx = kmalloc(len, GFP_KERNEL);
  1168. if (!ctx)
  1169. goto error_path;
  1170. len = security_inode_getsecurity(inode, (char *)security_inode_xattr_getsuffix(), ctx, len, 0);
  1171. if (len < 0)
  1172. goto error_path;
  1173. kfree(context->names[idx].ctx);
  1174. context->names[idx].ctx = ctx;
  1175. return;
  1176. error_path:
  1177. if (ctx)
  1178. kfree(ctx);
  1179. audit_panic("error in audit_inode_context");
  1180. return;
  1181. }
  1182. /**
  1183. * audit_inode - store the inode and device from a lookup
  1184. * @name: name being audited
  1185. * @inode: inode being audited
  1186. * @flags: lookup flags (as used in path_lookup())
  1187. *
  1188. * Called from fs/namei.c:path_lookup().
  1189. */
  1190. void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
  1191. {
  1192. int idx;
  1193. struct audit_context *context = current->audit_context;
  1194. if (!context->in_syscall)
  1195. return;
  1196. if (context->name_count
  1197. && context->names[context->name_count-1].name
  1198. && context->names[context->name_count-1].name == name)
  1199. idx = context->name_count - 1;
  1200. else if (context->name_count > 1
  1201. && context->names[context->name_count-2].name
  1202. && context->names[context->name_count-2].name == name)
  1203. idx = context->name_count - 2;
  1204. else {
  1205. /* FIXME: how much do we care about inodes that have no
  1206. * associated name? */
  1207. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  1208. return;
  1209. idx = context->name_count++;
  1210. context->names[idx].name = NULL;
  1211. #if AUDIT_DEBUG
  1212. ++context->ino_count;
  1213. #endif
  1214. }
  1215. context->names[idx].dev = inode->i_sb->s_dev;
  1216. context->names[idx].mode = inode->i_mode;
  1217. context->names[idx].uid = inode->i_uid;
  1218. context->names[idx].gid = inode->i_gid;
  1219. context->names[idx].rdev = inode->i_rdev;
  1220. audit_inode_context(idx, inode);
  1221. if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
  1222. (strcmp(name, ".") != 0)) {
  1223. context->names[idx].ino = (unsigned long)-1;
  1224. context->names[idx].pino = inode->i_ino;
  1225. } else {
  1226. context->names[idx].ino = inode->i_ino;
  1227. context->names[idx].pino = (unsigned long)-1;
  1228. }
  1229. }
  1230. /**
  1231. * audit_inode_child - collect inode info for created/removed objects
  1232. * @dname: inode's dentry name
  1233. * @inode: inode being audited
  1234. * @pino: inode number of dentry parent
  1235. *
  1236. * For syscalls that create or remove filesystem objects, audit_inode
  1237. * can only collect information for the filesystem object's parent.
  1238. * This call updates the audit context with the child's information.
  1239. * Syscalls that create a new filesystem object must be hooked after
  1240. * the object is created. Syscalls that remove a filesystem object
  1241. * must be hooked prior, in order to capture the target inode during
  1242. * unsuccessful attempts.
  1243. */
  1244. void __audit_inode_child(const char *dname, const struct inode *inode,
  1245. unsigned long pino)
  1246. {
  1247. int idx;
  1248. struct audit_context *context = current->audit_context;
  1249. if (!context->in_syscall)
  1250. return;
  1251. /* determine matching parent */
  1252. if (dname)
  1253. for (idx = 0; idx < context->name_count; idx++)
  1254. if (context->names[idx].pino == pino) {
  1255. const char *n;
  1256. const char *name = context->names[idx].name;
  1257. int dlen = strlen(dname);
  1258. int nlen = name ? strlen(name) : 0;
  1259. if (nlen < dlen)
  1260. continue;
  1261. /* disregard trailing slashes */
  1262. n = name + nlen - 1;
  1263. while ((*n == '/') && (n > name))
  1264. n--;
  1265. /* find last path component */
  1266. n = n - dlen + 1;
  1267. if (n < name)
  1268. continue;
  1269. else if (n > name) {
  1270. if (*--n != '/')
  1271. continue;
  1272. else
  1273. n++;
  1274. }
  1275. if (strncmp(n, dname, dlen) == 0)
  1276. goto update_context;
  1277. }
  1278. /* catch-all in case match not found */
  1279. idx = context->name_count++;
  1280. context->names[idx].name = NULL;
  1281. context->names[idx].pino = pino;
  1282. #if AUDIT_DEBUG
  1283. context->ino_count++;
  1284. #endif
  1285. update_context:
  1286. if (inode) {
  1287. context->names[idx].ino = inode->i_ino;
  1288. context->names[idx].dev = inode->i_sb->s_dev;
  1289. context->names[idx].mode = inode->i_mode;
  1290. context->names[idx].uid = inode->i_uid;
  1291. context->names[idx].gid = inode->i_gid;
  1292. context->names[idx].rdev = inode->i_rdev;
  1293. audit_inode_context(idx, inode);
  1294. }
  1295. }
  1296. /**
  1297. * auditsc_get_stamp - get local copies of audit_context values
  1298. * @ctx: audit_context for the task
  1299. * @t: timespec to store time recorded in the audit_context
  1300. * @serial: serial value that is recorded in the audit_context
  1301. *
  1302. * Also sets the context as auditable.
  1303. */
  1304. void auditsc_get_stamp(struct audit_context *ctx,
  1305. struct timespec *t, unsigned int *serial)
  1306. {
  1307. if (!ctx->serial)
  1308. ctx->serial = audit_serial();
  1309. t->tv_sec = ctx->ctime.tv_sec;
  1310. t->tv_nsec = ctx->ctime.tv_nsec;
  1311. *serial = ctx->serial;
  1312. ctx->auditable = 1;
  1313. }
  1314. /**
  1315. * audit_set_loginuid - set a task's audit_context loginuid
  1316. * @task: task whose audit context is being modified
  1317. * @loginuid: loginuid value
  1318. *
  1319. * Returns 0.
  1320. *
  1321. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1322. */
  1323. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1324. {
  1325. if (task->audit_context) {
  1326. struct audit_buffer *ab;
  1327. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1328. if (ab) {
  1329. audit_log_format(ab, "login pid=%d uid=%u "
  1330. "old auid=%u new auid=%u",
  1331. task->pid, task->uid,
  1332. task->audit_context->loginuid, loginuid);
  1333. audit_log_end(ab);
  1334. }
  1335. task->audit_context->loginuid = loginuid;
  1336. }
  1337. return 0;
  1338. }
  1339. /**
  1340. * audit_get_loginuid - get the loginuid for an audit_context
  1341. * @ctx: the audit_context
  1342. *
  1343. * Returns the context's loginuid or -1 if @ctx is NULL.
  1344. */
  1345. uid_t audit_get_loginuid(struct audit_context *ctx)
  1346. {
  1347. return ctx ? ctx->loginuid : -1;
  1348. }
  1349. static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
  1350. {
  1351. struct audit_context *context = current->audit_context;
  1352. char *ctx = NULL;
  1353. int len = 0;
  1354. if (likely(!context))
  1355. return NULL;
  1356. len = security_ipc_getsecurity(ipcp, NULL, 0);
  1357. if (len == -EOPNOTSUPP)
  1358. goto ret;
  1359. if (len < 0)
  1360. goto error_path;
  1361. ctx = kmalloc(len, GFP_ATOMIC);
  1362. if (!ctx)
  1363. goto error_path;
  1364. len = security_ipc_getsecurity(ipcp, ctx, len);
  1365. if (len < 0)
  1366. goto error_path;
  1367. return ctx;
  1368. error_path:
  1369. kfree(ctx);
  1370. audit_panic("error in audit_ipc_context");
  1371. ret:
  1372. return NULL;
  1373. }
  1374. /**
  1375. * audit_ipc_perms - record audit data for ipc
  1376. * @qbytes: msgq bytes
  1377. * @uid: msgq user id
  1378. * @gid: msgq group id
  1379. * @mode: msgq mode (permissions)
  1380. *
  1381. * Returns 0 for success or NULL context or < 0 on error.
  1382. */
  1383. int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
  1384. {
  1385. struct audit_aux_data_ipcctl *ax;
  1386. struct audit_context *context = current->audit_context;
  1387. if (likely(!context))
  1388. return 0;
  1389. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1390. if (!ax)
  1391. return -ENOMEM;
  1392. ax->qbytes = qbytes;
  1393. ax->uid = uid;
  1394. ax->gid = gid;
  1395. ax->mode = mode;
  1396. ax->ctx = audit_ipc_context(ipcp);
  1397. ax->d.type = AUDIT_IPC;
  1398. ax->d.next = context->aux;
  1399. context->aux = (void *)ax;
  1400. return 0;
  1401. }
  1402. /**
  1403. * audit_socketcall - record audit data for sys_socketcall
  1404. * @nargs: number of args
  1405. * @args: args array
  1406. *
  1407. * Returns 0 for success or NULL context or < 0 on error.
  1408. */
  1409. int audit_socketcall(int nargs, unsigned long *args)
  1410. {
  1411. struct audit_aux_data_socketcall *ax;
  1412. struct audit_context *context = current->audit_context;
  1413. if (likely(!context))
  1414. return 0;
  1415. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1416. if (!ax)
  1417. return -ENOMEM;
  1418. ax->nargs = nargs;
  1419. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1420. ax->d.type = AUDIT_SOCKETCALL;
  1421. ax->d.next = context->aux;
  1422. context->aux = (void *)ax;
  1423. return 0;
  1424. }
  1425. /**
  1426. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1427. * @len: data length in user space
  1428. * @a: data address in kernel space
  1429. *
  1430. * Returns 0 for success or NULL context or < 0 on error.
  1431. */
  1432. int audit_sockaddr(int len, void *a)
  1433. {
  1434. struct audit_aux_data_sockaddr *ax;
  1435. struct audit_context *context = current->audit_context;
  1436. if (likely(!context))
  1437. return 0;
  1438. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1439. if (!ax)
  1440. return -ENOMEM;
  1441. ax->len = len;
  1442. memcpy(ax->a, a, len);
  1443. ax->d.type = AUDIT_SOCKADDR;
  1444. ax->d.next = context->aux;
  1445. context->aux = (void *)ax;
  1446. return 0;
  1447. }
  1448. /**
  1449. * audit_avc_path - record the granting or denial of permissions
  1450. * @dentry: dentry to record
  1451. * @mnt: mnt to record
  1452. *
  1453. * Returns 0 for success or NULL context or < 0 on error.
  1454. *
  1455. * Called from security/selinux/avc.c::avc_audit()
  1456. */
  1457. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1458. {
  1459. struct audit_aux_data_path *ax;
  1460. struct audit_context *context = current->audit_context;
  1461. if (likely(!context))
  1462. return 0;
  1463. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1464. if (!ax)
  1465. return -ENOMEM;
  1466. ax->dentry = dget(dentry);
  1467. ax->mnt = mntget(mnt);
  1468. ax->d.type = AUDIT_AVC_PATH;
  1469. ax->d.next = context->aux;
  1470. context->aux = (void *)ax;
  1471. return 0;
  1472. }
  1473. /**
  1474. * audit_signal_info - record signal info for shutting down audit subsystem
  1475. * @sig: signal value
  1476. * @t: task being signaled
  1477. *
  1478. * If the audit subsystem is being terminated, record the task (pid)
  1479. * and uid that is doing that.
  1480. */
  1481. void audit_signal_info(int sig, struct task_struct *t)
  1482. {
  1483. extern pid_t audit_sig_pid;
  1484. extern uid_t audit_sig_uid;
  1485. if (unlikely(audit_pid && t->tgid == audit_pid)) {
  1486. if (sig == SIGTERM || sig == SIGHUP) {
  1487. struct audit_context *ctx = current->audit_context;
  1488. audit_sig_pid = current->pid;
  1489. if (ctx)
  1490. audit_sig_uid = ctx->loginuid;
  1491. else
  1492. audit_sig_uid = current->uid;
  1493. }
  1494. }
  1495. }