auditsc.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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, 2006 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. * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
  33. * 2006.
  34. *
  35. * The support of additional filter rules compares (>, <, >=, <=) was
  36. * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
  37. *
  38. * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
  39. * filesystem information.
  40. *
  41. * Subject and object context labeling support added by <danjones@us.ibm.com>
  42. * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
  43. */
  44. #include <linux/init.h>
  45. #include <asm/types.h>
  46. #include <asm/atomic.h>
  47. #include <asm/types.h>
  48. #include <linux/fs.h>
  49. #include <linux/namei.h>
  50. #include <linux/mm.h>
  51. #include <linux/module.h>
  52. #include <linux/mount.h>
  53. #include <linux/socket.h>
  54. #include <linux/mqueue.h>
  55. #include <linux/audit.h>
  56. #include <linux/personality.h>
  57. #include <linux/time.h>
  58. #include <linux/netlink.h>
  59. #include <linux/compiler.h>
  60. #include <asm/unistd.h>
  61. #include <linux/security.h>
  62. #include <linux/list.h>
  63. #include <linux/tty.h>
  64. #include <linux/selinux.h>
  65. #include <linux/binfmts.h>
  66. #include <linux/syscalls.h>
  67. #include "audit.h"
  68. extern struct list_head audit_filter_list[];
  69. /* No syscall auditing will take place unless audit_enabled != 0. */
  70. extern int audit_enabled;
  71. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  72. * for saving names from getname(). */
  73. #define AUDIT_NAMES 20
  74. /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
  75. * audit_context from being used for nameless inodes from
  76. * path_lookup. */
  77. #define AUDIT_NAMES_RESERVED 7
  78. /* Indicates that audit should log the full pathname. */
  79. #define AUDIT_NAME_FULL -1
  80. /* number of audit rules */
  81. int audit_n_rules;
  82. /* When fs/namei.c:getname() is called, we store the pointer in name and
  83. * we don't let putname() free it (instead we free all of the saved
  84. * pointers at syscall exit time).
  85. *
  86. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  87. struct audit_names {
  88. const char *name;
  89. int name_len; /* number of name's characters to log */
  90. unsigned name_put; /* call __putname() for this name */
  91. unsigned long ino;
  92. dev_t dev;
  93. umode_t mode;
  94. uid_t uid;
  95. gid_t gid;
  96. dev_t rdev;
  97. u32 osid;
  98. };
  99. struct audit_aux_data {
  100. struct audit_aux_data *next;
  101. int type;
  102. };
  103. #define AUDIT_AUX_IPCPERM 0
  104. struct audit_aux_data_mq_open {
  105. struct audit_aux_data d;
  106. int oflag;
  107. mode_t mode;
  108. struct mq_attr attr;
  109. };
  110. struct audit_aux_data_mq_sendrecv {
  111. struct audit_aux_data d;
  112. mqd_t mqdes;
  113. size_t msg_len;
  114. unsigned int msg_prio;
  115. struct timespec abs_timeout;
  116. };
  117. struct audit_aux_data_mq_notify {
  118. struct audit_aux_data d;
  119. mqd_t mqdes;
  120. struct sigevent notification;
  121. };
  122. struct audit_aux_data_mq_getsetattr {
  123. struct audit_aux_data d;
  124. mqd_t mqdes;
  125. struct mq_attr mqstat;
  126. };
  127. struct audit_aux_data_ipcctl {
  128. struct audit_aux_data d;
  129. struct ipc_perm p;
  130. unsigned long qbytes;
  131. uid_t uid;
  132. gid_t gid;
  133. mode_t mode;
  134. u32 osid;
  135. };
  136. struct audit_aux_data_execve {
  137. struct audit_aux_data d;
  138. int argc;
  139. int envc;
  140. char mem[0];
  141. };
  142. struct audit_aux_data_socketcall {
  143. struct audit_aux_data d;
  144. int nargs;
  145. unsigned long args[0];
  146. };
  147. struct audit_aux_data_sockaddr {
  148. struct audit_aux_data d;
  149. int len;
  150. char a[0];
  151. };
  152. struct audit_aux_data_path {
  153. struct audit_aux_data d;
  154. struct dentry *dentry;
  155. struct vfsmount *mnt;
  156. };
  157. /* The per-task audit context. */
  158. struct audit_context {
  159. int dummy; /* must be the first element */
  160. int in_syscall; /* 1 if task is in a syscall */
  161. enum audit_state state;
  162. unsigned int serial; /* serial number for record */
  163. struct timespec ctime; /* time of syscall entry */
  164. uid_t loginuid; /* login uid (identity) */
  165. int major; /* syscall number */
  166. unsigned long argv[4]; /* syscall arguments */
  167. int return_valid; /* return code is valid */
  168. long return_code;/* syscall return code */
  169. int auditable; /* 1 if record should be written */
  170. int name_count;
  171. struct audit_names names[AUDIT_NAMES];
  172. char * filterkey; /* key for rule that triggered record */
  173. struct dentry * pwd;
  174. struct vfsmount * pwdmnt;
  175. struct audit_context *previous; /* For nested syscalls */
  176. struct audit_aux_data *aux;
  177. /* Save things to print about task_struct */
  178. pid_t pid, ppid;
  179. uid_t uid, euid, suid, fsuid;
  180. gid_t gid, egid, sgid, fsgid;
  181. unsigned long personality;
  182. int arch;
  183. #if AUDIT_DEBUG
  184. int put_count;
  185. int ino_count;
  186. #endif
  187. };
  188. #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
  189. static inline int open_arg(int flags, int mask)
  190. {
  191. int n = ACC_MODE(flags);
  192. if (flags & (O_TRUNC | O_CREAT))
  193. n |= AUDIT_PERM_WRITE;
  194. return n & mask;
  195. }
  196. static int audit_match_perm(struct audit_context *ctx, int mask)
  197. {
  198. unsigned n = ctx->major;
  199. switch (audit_classify_syscall(ctx->arch, n)) {
  200. case 0: /* native */
  201. if ((mask & AUDIT_PERM_WRITE) &&
  202. audit_match_class(AUDIT_CLASS_WRITE, n))
  203. return 1;
  204. if ((mask & AUDIT_PERM_READ) &&
  205. audit_match_class(AUDIT_CLASS_READ, n))
  206. return 1;
  207. if ((mask & AUDIT_PERM_ATTR) &&
  208. audit_match_class(AUDIT_CLASS_CHATTR, n))
  209. return 1;
  210. return 0;
  211. case 1: /* 32bit on biarch */
  212. if ((mask & AUDIT_PERM_WRITE) &&
  213. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  214. return 1;
  215. if ((mask & AUDIT_PERM_READ) &&
  216. audit_match_class(AUDIT_CLASS_READ_32, n))
  217. return 1;
  218. if ((mask & AUDIT_PERM_ATTR) &&
  219. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  220. return 1;
  221. return 0;
  222. case 2: /* open */
  223. return mask & ACC_MODE(ctx->argv[1]);
  224. case 3: /* openat */
  225. return mask & ACC_MODE(ctx->argv[2]);
  226. case 4: /* socketcall */
  227. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  228. case 5: /* execve */
  229. return mask & AUDIT_PERM_EXEC;
  230. default:
  231. return 0;
  232. }
  233. }
  234. /* Determine if any context name data matches a rule's watch data */
  235. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  236. * otherwise. */
  237. static int audit_filter_rules(struct task_struct *tsk,
  238. struct audit_krule *rule,
  239. struct audit_context *ctx,
  240. struct audit_names *name,
  241. enum audit_state *state)
  242. {
  243. int i, j, need_sid = 1;
  244. u32 sid;
  245. for (i = 0; i < rule->field_count; i++) {
  246. struct audit_field *f = &rule->fields[i];
  247. int result = 0;
  248. switch (f->type) {
  249. case AUDIT_PID:
  250. result = audit_comparator(tsk->pid, f->op, f->val);
  251. break;
  252. case AUDIT_PPID:
  253. if (ctx)
  254. result = audit_comparator(ctx->ppid, f->op, f->val);
  255. break;
  256. case AUDIT_UID:
  257. result = audit_comparator(tsk->uid, f->op, f->val);
  258. break;
  259. case AUDIT_EUID:
  260. result = audit_comparator(tsk->euid, f->op, f->val);
  261. break;
  262. case AUDIT_SUID:
  263. result = audit_comparator(tsk->suid, f->op, f->val);
  264. break;
  265. case AUDIT_FSUID:
  266. result = audit_comparator(tsk->fsuid, f->op, f->val);
  267. break;
  268. case AUDIT_GID:
  269. result = audit_comparator(tsk->gid, f->op, f->val);
  270. break;
  271. case AUDIT_EGID:
  272. result = audit_comparator(tsk->egid, f->op, f->val);
  273. break;
  274. case AUDIT_SGID:
  275. result = audit_comparator(tsk->sgid, f->op, f->val);
  276. break;
  277. case AUDIT_FSGID:
  278. result = audit_comparator(tsk->fsgid, f->op, f->val);
  279. break;
  280. case AUDIT_PERS:
  281. result = audit_comparator(tsk->personality, f->op, f->val);
  282. break;
  283. case AUDIT_ARCH:
  284. if (ctx)
  285. result = audit_comparator(ctx->arch, f->op, f->val);
  286. break;
  287. case AUDIT_EXIT:
  288. if (ctx && ctx->return_valid)
  289. result = audit_comparator(ctx->return_code, f->op, f->val);
  290. break;
  291. case AUDIT_SUCCESS:
  292. if (ctx && ctx->return_valid) {
  293. if (f->val)
  294. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  295. else
  296. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  297. }
  298. break;
  299. case AUDIT_DEVMAJOR:
  300. if (name)
  301. result = audit_comparator(MAJOR(name->dev),
  302. f->op, f->val);
  303. else if (ctx) {
  304. for (j = 0; j < ctx->name_count; j++) {
  305. if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
  306. ++result;
  307. break;
  308. }
  309. }
  310. }
  311. break;
  312. case AUDIT_DEVMINOR:
  313. if (name)
  314. result = audit_comparator(MINOR(name->dev),
  315. f->op, f->val);
  316. else if (ctx) {
  317. for (j = 0; j < ctx->name_count; j++) {
  318. if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
  319. ++result;
  320. break;
  321. }
  322. }
  323. }
  324. break;
  325. case AUDIT_INODE:
  326. if (name)
  327. result = (name->ino == f->val);
  328. else if (ctx) {
  329. for (j = 0; j < ctx->name_count; j++) {
  330. if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
  331. ++result;
  332. break;
  333. }
  334. }
  335. }
  336. break;
  337. case AUDIT_WATCH:
  338. if (name && rule->watch->ino != (unsigned long)-1)
  339. result = (name->dev == rule->watch->dev &&
  340. name->ino == rule->watch->ino);
  341. break;
  342. case AUDIT_LOGINUID:
  343. result = 0;
  344. if (ctx)
  345. result = audit_comparator(ctx->loginuid, f->op, f->val);
  346. break;
  347. case AUDIT_SUBJ_USER:
  348. case AUDIT_SUBJ_ROLE:
  349. case AUDIT_SUBJ_TYPE:
  350. case AUDIT_SUBJ_SEN:
  351. case AUDIT_SUBJ_CLR:
  352. /* NOTE: this may return negative values indicating
  353. a temporary error. We simply treat this as a
  354. match for now to avoid losing information that
  355. may be wanted. An error message will also be
  356. logged upon error */
  357. if (f->se_rule) {
  358. if (need_sid) {
  359. selinux_get_task_sid(tsk, &sid);
  360. need_sid = 0;
  361. }
  362. result = selinux_audit_rule_match(sid, f->type,
  363. f->op,
  364. f->se_rule,
  365. ctx);
  366. }
  367. break;
  368. case AUDIT_OBJ_USER:
  369. case AUDIT_OBJ_ROLE:
  370. case AUDIT_OBJ_TYPE:
  371. case AUDIT_OBJ_LEV_LOW:
  372. case AUDIT_OBJ_LEV_HIGH:
  373. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  374. also applies here */
  375. if (f->se_rule) {
  376. /* Find files that match */
  377. if (name) {
  378. result = selinux_audit_rule_match(
  379. name->osid, f->type, f->op,
  380. f->se_rule, ctx);
  381. } else if (ctx) {
  382. for (j = 0; j < ctx->name_count; j++) {
  383. if (selinux_audit_rule_match(
  384. ctx->names[j].osid,
  385. f->type, f->op,
  386. f->se_rule, ctx)) {
  387. ++result;
  388. break;
  389. }
  390. }
  391. }
  392. /* Find ipc objects that match */
  393. if (ctx) {
  394. struct audit_aux_data *aux;
  395. for (aux = ctx->aux; aux;
  396. aux = aux->next) {
  397. if (aux->type == AUDIT_IPC) {
  398. struct audit_aux_data_ipcctl *axi = (void *)aux;
  399. if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
  400. ++result;
  401. break;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. break;
  408. case AUDIT_ARG0:
  409. case AUDIT_ARG1:
  410. case AUDIT_ARG2:
  411. case AUDIT_ARG3:
  412. if (ctx)
  413. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  414. break;
  415. case AUDIT_FILTERKEY:
  416. /* ignore this field for filtering */
  417. result = 1;
  418. break;
  419. case AUDIT_PERM:
  420. result = audit_match_perm(ctx, f->val);
  421. break;
  422. }
  423. if (!result)
  424. return 0;
  425. }
  426. if (rule->filterkey)
  427. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  428. switch (rule->action) {
  429. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  430. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  431. }
  432. return 1;
  433. }
  434. /* At process creation time, we can determine if system-call auditing is
  435. * completely disabled for this task. Since we only have the task
  436. * structure at this point, we can only check uid and gid.
  437. */
  438. static enum audit_state audit_filter_task(struct task_struct *tsk)
  439. {
  440. struct audit_entry *e;
  441. enum audit_state state;
  442. rcu_read_lock();
  443. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  444. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  445. rcu_read_unlock();
  446. return state;
  447. }
  448. }
  449. rcu_read_unlock();
  450. return AUDIT_BUILD_CONTEXT;
  451. }
  452. /* At syscall entry and exit time, this filter is called if the
  453. * audit_state is not low enough that auditing cannot take place, but is
  454. * also not high enough that we already know we have to write an audit
  455. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  456. */
  457. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  458. struct audit_context *ctx,
  459. struct list_head *list)
  460. {
  461. struct audit_entry *e;
  462. enum audit_state state;
  463. if (audit_pid && tsk->tgid == audit_pid)
  464. return AUDIT_DISABLED;
  465. rcu_read_lock();
  466. if (!list_empty(list)) {
  467. int word = AUDIT_WORD(ctx->major);
  468. int bit = AUDIT_BIT(ctx->major);
  469. list_for_each_entry_rcu(e, list, list) {
  470. if ((e->rule.mask[word] & bit) == bit &&
  471. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  472. &state)) {
  473. rcu_read_unlock();
  474. return state;
  475. }
  476. }
  477. }
  478. rcu_read_unlock();
  479. return AUDIT_BUILD_CONTEXT;
  480. }
  481. /* At syscall exit time, this filter is called if any audit_names[] have been
  482. * collected during syscall processing. We only check rules in sublists at hash
  483. * buckets applicable to the inode numbers in audit_names[].
  484. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  485. */
  486. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  487. struct audit_context *ctx)
  488. {
  489. int i;
  490. struct audit_entry *e;
  491. enum audit_state state;
  492. if (audit_pid && tsk->tgid == audit_pid)
  493. return AUDIT_DISABLED;
  494. rcu_read_lock();
  495. for (i = 0; i < ctx->name_count; i++) {
  496. int word = AUDIT_WORD(ctx->major);
  497. int bit = AUDIT_BIT(ctx->major);
  498. struct audit_names *n = &ctx->names[i];
  499. int h = audit_hash_ino((u32)n->ino);
  500. struct list_head *list = &audit_inode_hash[h];
  501. if (list_empty(list))
  502. continue;
  503. list_for_each_entry_rcu(e, list, list) {
  504. if ((e->rule.mask[word] & bit) == bit &&
  505. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  506. rcu_read_unlock();
  507. return state;
  508. }
  509. }
  510. }
  511. rcu_read_unlock();
  512. return AUDIT_BUILD_CONTEXT;
  513. }
  514. void audit_set_auditable(struct audit_context *ctx)
  515. {
  516. ctx->auditable = 1;
  517. }
  518. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  519. int return_valid,
  520. int return_code)
  521. {
  522. struct audit_context *context = tsk->audit_context;
  523. if (likely(!context))
  524. return NULL;
  525. context->return_valid = return_valid;
  526. context->return_code = return_code;
  527. if (context->in_syscall && !context->dummy && !context->auditable) {
  528. enum audit_state state;
  529. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  530. if (state == AUDIT_RECORD_CONTEXT) {
  531. context->auditable = 1;
  532. goto get_context;
  533. }
  534. state = audit_filter_inodes(tsk, context);
  535. if (state == AUDIT_RECORD_CONTEXT)
  536. context->auditable = 1;
  537. }
  538. get_context:
  539. tsk->audit_context = NULL;
  540. return context;
  541. }
  542. static inline void audit_free_names(struct audit_context *context)
  543. {
  544. int i;
  545. #if AUDIT_DEBUG == 2
  546. if (context->auditable
  547. ||context->put_count + context->ino_count != context->name_count) {
  548. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  549. " name_count=%d put_count=%d"
  550. " ino_count=%d [NOT freeing]\n",
  551. __FILE__, __LINE__,
  552. context->serial, context->major, context->in_syscall,
  553. context->name_count, context->put_count,
  554. context->ino_count);
  555. for (i = 0; i < context->name_count; i++) {
  556. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  557. context->names[i].name,
  558. context->names[i].name ?: "(null)");
  559. }
  560. dump_stack();
  561. return;
  562. }
  563. #endif
  564. #if AUDIT_DEBUG
  565. context->put_count = 0;
  566. context->ino_count = 0;
  567. #endif
  568. for (i = 0; i < context->name_count; i++) {
  569. if (context->names[i].name && context->names[i].name_put)
  570. __putname(context->names[i].name);
  571. }
  572. context->name_count = 0;
  573. if (context->pwd)
  574. dput(context->pwd);
  575. if (context->pwdmnt)
  576. mntput(context->pwdmnt);
  577. context->pwd = NULL;
  578. context->pwdmnt = NULL;
  579. }
  580. static inline void audit_free_aux(struct audit_context *context)
  581. {
  582. struct audit_aux_data *aux;
  583. while ((aux = context->aux)) {
  584. if (aux->type == AUDIT_AVC_PATH) {
  585. struct audit_aux_data_path *axi = (void *)aux;
  586. dput(axi->dentry);
  587. mntput(axi->mnt);
  588. }
  589. context->aux = aux->next;
  590. kfree(aux);
  591. }
  592. }
  593. static inline void audit_zero_context(struct audit_context *context,
  594. enum audit_state state)
  595. {
  596. uid_t loginuid = context->loginuid;
  597. memset(context, 0, sizeof(*context));
  598. context->state = state;
  599. context->loginuid = loginuid;
  600. }
  601. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  602. {
  603. struct audit_context *context;
  604. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  605. return NULL;
  606. audit_zero_context(context, state);
  607. return context;
  608. }
  609. /**
  610. * audit_alloc - allocate an audit context block for a task
  611. * @tsk: task
  612. *
  613. * Filter on the task information and allocate a per-task audit context
  614. * if necessary. Doing so turns on system call auditing for the
  615. * specified task. This is called from copy_process, so no lock is
  616. * needed.
  617. */
  618. int audit_alloc(struct task_struct *tsk)
  619. {
  620. struct audit_context *context;
  621. enum audit_state state;
  622. if (likely(!audit_enabled))
  623. return 0; /* Return if not auditing. */
  624. state = audit_filter_task(tsk);
  625. if (likely(state == AUDIT_DISABLED))
  626. return 0;
  627. if (!(context = audit_alloc_context(state))) {
  628. audit_log_lost("out of memory in audit_alloc");
  629. return -ENOMEM;
  630. }
  631. /* Preserve login uid */
  632. context->loginuid = -1;
  633. if (current->audit_context)
  634. context->loginuid = current->audit_context->loginuid;
  635. tsk->audit_context = context;
  636. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  637. return 0;
  638. }
  639. static inline void audit_free_context(struct audit_context *context)
  640. {
  641. struct audit_context *previous;
  642. int count = 0;
  643. do {
  644. previous = context->previous;
  645. if (previous || (count && count < 10)) {
  646. ++count;
  647. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  648. " freeing multiple contexts (%d)\n",
  649. context->serial, context->major,
  650. context->name_count, count);
  651. }
  652. audit_free_names(context);
  653. audit_free_aux(context);
  654. kfree(context->filterkey);
  655. kfree(context);
  656. context = previous;
  657. } while (context);
  658. if (count >= 10)
  659. printk(KERN_ERR "audit: freed %d contexts\n", count);
  660. }
  661. static void audit_log_task_context(struct audit_buffer *ab)
  662. {
  663. char *ctx = NULL;
  664. ssize_t len = 0;
  665. len = security_getprocattr(current, "current", NULL, 0);
  666. if (len < 0) {
  667. if (len != -EINVAL)
  668. goto error_path;
  669. return;
  670. }
  671. ctx = kmalloc(len, GFP_KERNEL);
  672. if (!ctx)
  673. goto error_path;
  674. len = security_getprocattr(current, "current", ctx, len);
  675. if (len < 0 )
  676. goto error_path;
  677. audit_log_format(ab, " subj=%s", ctx);
  678. return;
  679. error_path:
  680. kfree(ctx);
  681. audit_panic("error in audit_log_task_context");
  682. return;
  683. }
  684. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  685. {
  686. char name[sizeof(tsk->comm)];
  687. struct mm_struct *mm = tsk->mm;
  688. struct vm_area_struct *vma;
  689. /* tsk == current */
  690. get_task_comm(name, tsk);
  691. audit_log_format(ab, " comm=");
  692. audit_log_untrustedstring(ab, name);
  693. if (mm) {
  694. down_read(&mm->mmap_sem);
  695. vma = mm->mmap;
  696. while (vma) {
  697. if ((vma->vm_flags & VM_EXECUTABLE) &&
  698. vma->vm_file) {
  699. audit_log_d_path(ab, "exe=",
  700. vma->vm_file->f_dentry,
  701. vma->vm_file->f_vfsmnt);
  702. break;
  703. }
  704. vma = vma->vm_next;
  705. }
  706. up_read(&mm->mmap_sem);
  707. }
  708. audit_log_task_context(ab);
  709. }
  710. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  711. {
  712. int i, call_panic = 0;
  713. struct audit_buffer *ab;
  714. struct audit_aux_data *aux;
  715. const char *tty;
  716. /* tsk == current */
  717. context->pid = tsk->pid;
  718. context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
  719. context->uid = tsk->uid;
  720. context->gid = tsk->gid;
  721. context->euid = tsk->euid;
  722. context->suid = tsk->suid;
  723. context->fsuid = tsk->fsuid;
  724. context->egid = tsk->egid;
  725. context->sgid = tsk->sgid;
  726. context->fsgid = tsk->fsgid;
  727. context->personality = tsk->personality;
  728. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  729. if (!ab)
  730. return; /* audit_panic has been called */
  731. audit_log_format(ab, "arch=%x syscall=%d",
  732. context->arch, context->major);
  733. if (context->personality != PER_LINUX)
  734. audit_log_format(ab, " per=%lx", context->personality);
  735. if (context->return_valid)
  736. audit_log_format(ab, " success=%s exit=%ld",
  737. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  738. context->return_code);
  739. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  740. tty = tsk->signal->tty->name;
  741. else
  742. tty = "(none)";
  743. audit_log_format(ab,
  744. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  745. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  746. " euid=%u suid=%u fsuid=%u"
  747. " egid=%u sgid=%u fsgid=%u tty=%s",
  748. context->argv[0],
  749. context->argv[1],
  750. context->argv[2],
  751. context->argv[3],
  752. context->name_count,
  753. context->ppid,
  754. context->pid,
  755. context->loginuid,
  756. context->uid,
  757. context->gid,
  758. context->euid, context->suid, context->fsuid,
  759. context->egid, context->sgid, context->fsgid, tty);
  760. audit_log_task_info(ab, tsk);
  761. if (context->filterkey) {
  762. audit_log_format(ab, " key=");
  763. audit_log_untrustedstring(ab, context->filterkey);
  764. } else
  765. audit_log_format(ab, " key=(null)");
  766. audit_log_end(ab);
  767. for (aux = context->aux; aux; aux = aux->next) {
  768. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  769. if (!ab)
  770. continue; /* audit_panic has been called */
  771. switch (aux->type) {
  772. case AUDIT_MQ_OPEN: {
  773. struct audit_aux_data_mq_open *axi = (void *)aux;
  774. audit_log_format(ab,
  775. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  776. "mq_msgsize=%ld mq_curmsgs=%ld",
  777. axi->oflag, axi->mode, axi->attr.mq_flags,
  778. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  779. axi->attr.mq_curmsgs);
  780. break; }
  781. case AUDIT_MQ_SENDRECV: {
  782. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  783. audit_log_format(ab,
  784. "mqdes=%d msg_len=%zd msg_prio=%u "
  785. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  786. axi->mqdes, axi->msg_len, axi->msg_prio,
  787. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  788. break; }
  789. case AUDIT_MQ_NOTIFY: {
  790. struct audit_aux_data_mq_notify *axi = (void *)aux;
  791. audit_log_format(ab,
  792. "mqdes=%d sigev_signo=%d",
  793. axi->mqdes,
  794. axi->notification.sigev_signo);
  795. break; }
  796. case AUDIT_MQ_GETSETATTR: {
  797. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  798. audit_log_format(ab,
  799. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  800. "mq_curmsgs=%ld ",
  801. axi->mqdes,
  802. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  803. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  804. break; }
  805. case AUDIT_IPC: {
  806. struct audit_aux_data_ipcctl *axi = (void *)aux;
  807. audit_log_format(ab,
  808. "ouid=%u ogid=%u mode=%x",
  809. axi->uid, axi->gid, axi->mode);
  810. if (axi->osid != 0) {
  811. char *ctx = NULL;
  812. u32 len;
  813. if (selinux_sid_to_string(
  814. axi->osid, &ctx, &len)) {
  815. audit_log_format(ab, " osid=%u",
  816. axi->osid);
  817. call_panic = 1;
  818. } else
  819. audit_log_format(ab, " obj=%s", ctx);
  820. kfree(ctx);
  821. }
  822. break; }
  823. case AUDIT_IPC_SET_PERM: {
  824. struct audit_aux_data_ipcctl *axi = (void *)aux;
  825. audit_log_format(ab,
  826. "qbytes=%lx ouid=%u ogid=%u mode=%x",
  827. axi->qbytes, axi->uid, axi->gid, axi->mode);
  828. break; }
  829. case AUDIT_EXECVE: {
  830. struct audit_aux_data_execve *axi = (void *)aux;
  831. int i;
  832. const char *p;
  833. for (i = 0, p = axi->mem; i < axi->argc; i++) {
  834. audit_log_format(ab, "a%d=", i);
  835. p = audit_log_untrustedstring(ab, p);
  836. audit_log_format(ab, "\n");
  837. }
  838. break; }
  839. case AUDIT_SOCKETCALL: {
  840. int i;
  841. struct audit_aux_data_socketcall *axs = (void *)aux;
  842. audit_log_format(ab, "nargs=%d", axs->nargs);
  843. for (i=0; i<axs->nargs; i++)
  844. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  845. break; }
  846. case AUDIT_SOCKADDR: {
  847. struct audit_aux_data_sockaddr *axs = (void *)aux;
  848. audit_log_format(ab, "saddr=");
  849. audit_log_hex(ab, axs->a, axs->len);
  850. break; }
  851. case AUDIT_AVC_PATH: {
  852. struct audit_aux_data_path *axi = (void *)aux;
  853. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  854. break; }
  855. }
  856. audit_log_end(ab);
  857. }
  858. if (context->pwd && context->pwdmnt) {
  859. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  860. if (ab) {
  861. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  862. audit_log_end(ab);
  863. }
  864. }
  865. for (i = 0; i < context->name_count; i++) {
  866. struct audit_names *n = &context->names[i];
  867. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  868. if (!ab)
  869. continue; /* audit_panic has been called */
  870. audit_log_format(ab, "item=%d", i);
  871. if (n->name) {
  872. switch(n->name_len) {
  873. case AUDIT_NAME_FULL:
  874. /* log the full path */
  875. audit_log_format(ab, " name=");
  876. audit_log_untrustedstring(ab, n->name);
  877. break;
  878. case 0:
  879. /* name was specified as a relative path and the
  880. * directory component is the cwd */
  881. audit_log_d_path(ab, " name=", context->pwd,
  882. context->pwdmnt);
  883. break;
  884. default:
  885. /* log the name's directory component */
  886. audit_log_format(ab, " name=");
  887. audit_log_n_untrustedstring(ab, n->name_len,
  888. n->name);
  889. }
  890. } else
  891. audit_log_format(ab, " name=(null)");
  892. if (n->ino != (unsigned long)-1) {
  893. audit_log_format(ab, " inode=%lu"
  894. " dev=%02x:%02x mode=%#o"
  895. " ouid=%u ogid=%u rdev=%02x:%02x",
  896. n->ino,
  897. MAJOR(n->dev),
  898. MINOR(n->dev),
  899. n->mode,
  900. n->uid,
  901. n->gid,
  902. MAJOR(n->rdev),
  903. MINOR(n->rdev));
  904. }
  905. if (n->osid != 0) {
  906. char *ctx = NULL;
  907. u32 len;
  908. if (selinux_sid_to_string(
  909. n->osid, &ctx, &len)) {
  910. audit_log_format(ab, " osid=%u", n->osid);
  911. call_panic = 2;
  912. } else
  913. audit_log_format(ab, " obj=%s", ctx);
  914. kfree(ctx);
  915. }
  916. audit_log_end(ab);
  917. }
  918. if (call_panic)
  919. audit_panic("error converting sid to string");
  920. }
  921. /**
  922. * audit_free - free a per-task audit context
  923. * @tsk: task whose audit context block to free
  924. *
  925. * Called from copy_process and do_exit
  926. */
  927. void audit_free(struct task_struct *tsk)
  928. {
  929. struct audit_context *context;
  930. context = audit_get_context(tsk, 0, 0);
  931. if (likely(!context))
  932. return;
  933. /* Check for system calls that do not go through the exit
  934. * function (e.g., exit_group), then free context block.
  935. * We use GFP_ATOMIC here because we might be doing this
  936. * in the context of the idle thread */
  937. /* that can happen only if we are called from do_exit() */
  938. if (context->in_syscall && context->auditable)
  939. audit_log_exit(context, tsk);
  940. audit_free_context(context);
  941. }
  942. /**
  943. * audit_syscall_entry - fill in an audit record at syscall entry
  944. * @tsk: task being audited
  945. * @arch: architecture type
  946. * @major: major syscall type (function)
  947. * @a1: additional syscall register 1
  948. * @a2: additional syscall register 2
  949. * @a3: additional syscall register 3
  950. * @a4: additional syscall register 4
  951. *
  952. * Fill in audit context at syscall entry. This only happens if the
  953. * audit context was created when the task was created and the state or
  954. * filters demand the audit context be built. If the state from the
  955. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  956. * then the record will be written at syscall exit time (otherwise, it
  957. * will only be written if another part of the kernel requests that it
  958. * be written).
  959. */
  960. void audit_syscall_entry(int arch, int major,
  961. unsigned long a1, unsigned long a2,
  962. unsigned long a3, unsigned long a4)
  963. {
  964. struct task_struct *tsk = current;
  965. struct audit_context *context = tsk->audit_context;
  966. enum audit_state state;
  967. BUG_ON(!context);
  968. /*
  969. * This happens only on certain architectures that make system
  970. * calls in kernel_thread via the entry.S interface, instead of
  971. * with direct calls. (If you are porting to a new
  972. * architecture, hitting this condition can indicate that you
  973. * got the _exit/_leave calls backward in entry.S.)
  974. *
  975. * i386 no
  976. * x86_64 no
  977. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  978. *
  979. * This also happens with vm86 emulation in a non-nested manner
  980. * (entries without exits), so this case must be caught.
  981. */
  982. if (context->in_syscall) {
  983. struct audit_context *newctx;
  984. #if AUDIT_DEBUG
  985. printk(KERN_ERR
  986. "audit(:%d) pid=%d in syscall=%d;"
  987. " entering syscall=%d\n",
  988. context->serial, tsk->pid, context->major, major);
  989. #endif
  990. newctx = audit_alloc_context(context->state);
  991. if (newctx) {
  992. newctx->previous = context;
  993. context = newctx;
  994. tsk->audit_context = newctx;
  995. } else {
  996. /* If we can't alloc a new context, the best we
  997. * can do is to leak memory (any pending putname
  998. * will be lost). The only other alternative is
  999. * to abandon auditing. */
  1000. audit_zero_context(context, context->state);
  1001. }
  1002. }
  1003. BUG_ON(context->in_syscall || context->name_count);
  1004. if (!audit_enabled)
  1005. return;
  1006. context->arch = arch;
  1007. context->major = major;
  1008. context->argv[0] = a1;
  1009. context->argv[1] = a2;
  1010. context->argv[2] = a3;
  1011. context->argv[3] = a4;
  1012. state = context->state;
  1013. context->dummy = !audit_n_rules;
  1014. if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
  1015. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1016. if (likely(state == AUDIT_DISABLED))
  1017. return;
  1018. context->serial = 0;
  1019. context->ctime = CURRENT_TIME;
  1020. context->in_syscall = 1;
  1021. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  1022. }
  1023. /**
  1024. * audit_syscall_exit - deallocate audit context after a system call
  1025. * @tsk: task being audited
  1026. * @valid: success/failure flag
  1027. * @return_code: syscall return value
  1028. *
  1029. * Tear down after system call. If the audit context has been marked as
  1030. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1031. * filtering, or because some other part of the kernel write an audit
  1032. * message), then write out the syscall information. In call cases,
  1033. * free the names stored from getname().
  1034. */
  1035. void audit_syscall_exit(int valid, long return_code)
  1036. {
  1037. struct task_struct *tsk = current;
  1038. struct audit_context *context;
  1039. context = audit_get_context(tsk, valid, return_code);
  1040. if (likely(!context))
  1041. return;
  1042. if (context->in_syscall && context->auditable)
  1043. audit_log_exit(context, tsk);
  1044. context->in_syscall = 0;
  1045. context->auditable = 0;
  1046. if (context->previous) {
  1047. struct audit_context *new_context = context->previous;
  1048. context->previous = NULL;
  1049. audit_free_context(context);
  1050. tsk->audit_context = new_context;
  1051. } else {
  1052. audit_free_names(context);
  1053. audit_free_aux(context);
  1054. kfree(context->filterkey);
  1055. context->filterkey = NULL;
  1056. tsk->audit_context = context;
  1057. }
  1058. }
  1059. /**
  1060. * audit_getname - add a name to the list
  1061. * @name: name to add
  1062. *
  1063. * Add a name to the list of audit names for this context.
  1064. * Called from fs/namei.c:getname().
  1065. */
  1066. void __audit_getname(const char *name)
  1067. {
  1068. struct audit_context *context = current->audit_context;
  1069. if (IS_ERR(name) || !name)
  1070. return;
  1071. if (!context->in_syscall) {
  1072. #if AUDIT_DEBUG == 2
  1073. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1074. __FILE__, __LINE__, context->serial, name);
  1075. dump_stack();
  1076. #endif
  1077. return;
  1078. }
  1079. BUG_ON(context->name_count >= AUDIT_NAMES);
  1080. context->names[context->name_count].name = name;
  1081. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  1082. context->names[context->name_count].name_put = 1;
  1083. context->names[context->name_count].ino = (unsigned long)-1;
  1084. ++context->name_count;
  1085. if (!context->pwd) {
  1086. read_lock(&current->fs->lock);
  1087. context->pwd = dget(current->fs->pwd);
  1088. context->pwdmnt = mntget(current->fs->pwdmnt);
  1089. read_unlock(&current->fs->lock);
  1090. }
  1091. }
  1092. /* audit_putname - intercept a putname request
  1093. * @name: name to intercept and delay for putname
  1094. *
  1095. * If we have stored the name from getname in the audit context,
  1096. * then we delay the putname until syscall exit.
  1097. * Called from include/linux/fs.h:putname().
  1098. */
  1099. void audit_putname(const char *name)
  1100. {
  1101. struct audit_context *context = current->audit_context;
  1102. BUG_ON(!context);
  1103. if (!context->in_syscall) {
  1104. #if AUDIT_DEBUG == 2
  1105. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1106. __FILE__, __LINE__, context->serial, name);
  1107. if (context->name_count) {
  1108. int i;
  1109. for (i = 0; i < context->name_count; i++)
  1110. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1111. context->names[i].name,
  1112. context->names[i].name ?: "(null)");
  1113. }
  1114. #endif
  1115. __putname(name);
  1116. }
  1117. #if AUDIT_DEBUG
  1118. else {
  1119. ++context->put_count;
  1120. if (context->put_count > context->name_count) {
  1121. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1122. " in_syscall=%d putname(%p) name_count=%d"
  1123. " put_count=%d\n",
  1124. __FILE__, __LINE__,
  1125. context->serial, context->major,
  1126. context->in_syscall, name, context->name_count,
  1127. context->put_count);
  1128. dump_stack();
  1129. }
  1130. }
  1131. #endif
  1132. }
  1133. /* Copy inode data into an audit_names. */
  1134. static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
  1135. {
  1136. name->ino = inode->i_ino;
  1137. name->dev = inode->i_sb->s_dev;
  1138. name->mode = inode->i_mode;
  1139. name->uid = inode->i_uid;
  1140. name->gid = inode->i_gid;
  1141. name->rdev = inode->i_rdev;
  1142. selinux_get_inode_sid(inode, &name->osid);
  1143. }
  1144. /**
  1145. * audit_inode - store the inode and device from a lookup
  1146. * @name: name being audited
  1147. * @inode: inode being audited
  1148. *
  1149. * Called from fs/namei.c:path_lookup().
  1150. */
  1151. void __audit_inode(const char *name, const struct inode *inode)
  1152. {
  1153. int idx;
  1154. struct audit_context *context = current->audit_context;
  1155. if (!context->in_syscall)
  1156. return;
  1157. if (context->name_count
  1158. && context->names[context->name_count-1].name
  1159. && context->names[context->name_count-1].name == name)
  1160. idx = context->name_count - 1;
  1161. else if (context->name_count > 1
  1162. && context->names[context->name_count-2].name
  1163. && context->names[context->name_count-2].name == name)
  1164. idx = context->name_count - 2;
  1165. else {
  1166. /* FIXME: how much do we care about inodes that have no
  1167. * associated name? */
  1168. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  1169. return;
  1170. idx = context->name_count++;
  1171. context->names[idx].name = NULL;
  1172. #if AUDIT_DEBUG
  1173. ++context->ino_count;
  1174. #endif
  1175. }
  1176. audit_copy_inode(&context->names[idx], inode);
  1177. }
  1178. /**
  1179. * audit_inode_child - collect inode info for created/removed objects
  1180. * @dname: inode's dentry name
  1181. * @inode: inode being audited
  1182. * @parent: inode of dentry parent
  1183. *
  1184. * For syscalls that create or remove filesystem objects, audit_inode
  1185. * can only collect information for the filesystem object's parent.
  1186. * This call updates the audit context with the child's information.
  1187. * Syscalls that create a new filesystem object must be hooked after
  1188. * the object is created. Syscalls that remove a filesystem object
  1189. * must be hooked prior, in order to capture the target inode during
  1190. * unsuccessful attempts.
  1191. */
  1192. void __audit_inode_child(const char *dname, const struct inode *inode,
  1193. const struct inode *parent)
  1194. {
  1195. int idx;
  1196. struct audit_context *context = current->audit_context;
  1197. const char *found_name = NULL;
  1198. int dirlen = 0;
  1199. if (!context->in_syscall)
  1200. return;
  1201. /* determine matching parent */
  1202. if (!dname)
  1203. goto update_context;
  1204. for (idx = 0; idx < context->name_count; idx++)
  1205. if (context->names[idx].ino == parent->i_ino) {
  1206. const char *name = context->names[idx].name;
  1207. if (!name)
  1208. continue;
  1209. if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
  1210. context->names[idx].name_len = dirlen;
  1211. found_name = name;
  1212. break;
  1213. }
  1214. }
  1215. update_context:
  1216. idx = context->name_count++;
  1217. #if AUDIT_DEBUG
  1218. context->ino_count++;
  1219. #endif
  1220. /* Re-use the name belonging to the slot for a matching parent directory.
  1221. * All names for this context are relinquished in audit_free_names() */
  1222. context->names[idx].name = found_name;
  1223. context->names[idx].name_len = AUDIT_NAME_FULL;
  1224. context->names[idx].name_put = 0; /* don't call __putname() */
  1225. if (!inode)
  1226. context->names[idx].ino = (unsigned long)-1;
  1227. else
  1228. audit_copy_inode(&context->names[idx], inode);
  1229. /* A parent was not found in audit_names, so copy the inode data for the
  1230. * provided parent. */
  1231. if (!found_name) {
  1232. idx = context->name_count++;
  1233. #if AUDIT_DEBUG
  1234. context->ino_count++;
  1235. #endif
  1236. audit_copy_inode(&context->names[idx], parent);
  1237. }
  1238. }
  1239. /**
  1240. * audit_inode_update - update inode info for last collected name
  1241. * @inode: inode being audited
  1242. *
  1243. * When open() is called on an existing object with the O_CREAT flag, the inode
  1244. * data audit initially collects is incorrect. This additional hook ensures
  1245. * audit has the inode data for the actual object to be opened.
  1246. */
  1247. void __audit_inode_update(const struct inode *inode)
  1248. {
  1249. struct audit_context *context = current->audit_context;
  1250. int idx;
  1251. if (!context->in_syscall || !inode)
  1252. return;
  1253. if (context->name_count == 0) {
  1254. context->name_count++;
  1255. #if AUDIT_DEBUG
  1256. context->ino_count++;
  1257. #endif
  1258. }
  1259. idx = context->name_count - 1;
  1260. audit_copy_inode(&context->names[idx], inode);
  1261. }
  1262. /**
  1263. * auditsc_get_stamp - get local copies of audit_context values
  1264. * @ctx: audit_context for the task
  1265. * @t: timespec to store time recorded in the audit_context
  1266. * @serial: serial value that is recorded in the audit_context
  1267. *
  1268. * Also sets the context as auditable.
  1269. */
  1270. void auditsc_get_stamp(struct audit_context *ctx,
  1271. struct timespec *t, unsigned int *serial)
  1272. {
  1273. if (!ctx->serial)
  1274. ctx->serial = audit_serial();
  1275. t->tv_sec = ctx->ctime.tv_sec;
  1276. t->tv_nsec = ctx->ctime.tv_nsec;
  1277. *serial = ctx->serial;
  1278. ctx->auditable = 1;
  1279. }
  1280. /**
  1281. * audit_set_loginuid - set a task's audit_context loginuid
  1282. * @task: task whose audit context is being modified
  1283. * @loginuid: loginuid value
  1284. *
  1285. * Returns 0.
  1286. *
  1287. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1288. */
  1289. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1290. {
  1291. struct audit_context *context = task->audit_context;
  1292. if (context) {
  1293. /* Only log if audit is enabled */
  1294. if (context->in_syscall) {
  1295. struct audit_buffer *ab;
  1296. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1297. if (ab) {
  1298. audit_log_format(ab, "login pid=%d uid=%u "
  1299. "old auid=%u new auid=%u",
  1300. task->pid, task->uid,
  1301. context->loginuid, loginuid);
  1302. audit_log_end(ab);
  1303. }
  1304. }
  1305. context->loginuid = loginuid;
  1306. }
  1307. return 0;
  1308. }
  1309. /**
  1310. * audit_get_loginuid - get the loginuid for an audit_context
  1311. * @ctx: the audit_context
  1312. *
  1313. * Returns the context's loginuid or -1 if @ctx is NULL.
  1314. */
  1315. uid_t audit_get_loginuid(struct audit_context *ctx)
  1316. {
  1317. return ctx ? ctx->loginuid : -1;
  1318. }
  1319. /**
  1320. * __audit_mq_open - record audit data for a POSIX MQ open
  1321. * @oflag: open flag
  1322. * @mode: mode bits
  1323. * @u_attr: queue attributes
  1324. *
  1325. * Returns 0 for success or NULL context or < 0 on error.
  1326. */
  1327. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1328. {
  1329. struct audit_aux_data_mq_open *ax;
  1330. struct audit_context *context = current->audit_context;
  1331. if (!audit_enabled)
  1332. return 0;
  1333. if (likely(!context))
  1334. return 0;
  1335. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1336. if (!ax)
  1337. return -ENOMEM;
  1338. if (u_attr != NULL) {
  1339. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1340. kfree(ax);
  1341. return -EFAULT;
  1342. }
  1343. } else
  1344. memset(&ax->attr, 0, sizeof(ax->attr));
  1345. ax->oflag = oflag;
  1346. ax->mode = mode;
  1347. ax->d.type = AUDIT_MQ_OPEN;
  1348. ax->d.next = context->aux;
  1349. context->aux = (void *)ax;
  1350. return 0;
  1351. }
  1352. /**
  1353. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1354. * @mqdes: MQ descriptor
  1355. * @msg_len: Message length
  1356. * @msg_prio: Message priority
  1357. * @u_abs_timeout: Message timeout in absolute time
  1358. *
  1359. * Returns 0 for success or NULL context or < 0 on error.
  1360. */
  1361. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1362. const struct timespec __user *u_abs_timeout)
  1363. {
  1364. struct audit_aux_data_mq_sendrecv *ax;
  1365. struct audit_context *context = current->audit_context;
  1366. if (!audit_enabled)
  1367. return 0;
  1368. if (likely(!context))
  1369. return 0;
  1370. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1371. if (!ax)
  1372. return -ENOMEM;
  1373. if (u_abs_timeout != NULL) {
  1374. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1375. kfree(ax);
  1376. return -EFAULT;
  1377. }
  1378. } else
  1379. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1380. ax->mqdes = mqdes;
  1381. ax->msg_len = msg_len;
  1382. ax->msg_prio = msg_prio;
  1383. ax->d.type = AUDIT_MQ_SENDRECV;
  1384. ax->d.next = context->aux;
  1385. context->aux = (void *)ax;
  1386. return 0;
  1387. }
  1388. /**
  1389. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1390. * @mqdes: MQ descriptor
  1391. * @msg_len: Message length
  1392. * @u_msg_prio: Message priority
  1393. * @u_abs_timeout: Message timeout in absolute time
  1394. *
  1395. * Returns 0 for success or NULL context or < 0 on error.
  1396. */
  1397. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1398. unsigned int __user *u_msg_prio,
  1399. const struct timespec __user *u_abs_timeout)
  1400. {
  1401. struct audit_aux_data_mq_sendrecv *ax;
  1402. struct audit_context *context = current->audit_context;
  1403. if (!audit_enabled)
  1404. return 0;
  1405. if (likely(!context))
  1406. return 0;
  1407. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1408. if (!ax)
  1409. return -ENOMEM;
  1410. if (u_msg_prio != NULL) {
  1411. if (get_user(ax->msg_prio, u_msg_prio)) {
  1412. kfree(ax);
  1413. return -EFAULT;
  1414. }
  1415. } else
  1416. ax->msg_prio = 0;
  1417. if (u_abs_timeout != NULL) {
  1418. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1419. kfree(ax);
  1420. return -EFAULT;
  1421. }
  1422. } else
  1423. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1424. ax->mqdes = mqdes;
  1425. ax->msg_len = msg_len;
  1426. ax->d.type = AUDIT_MQ_SENDRECV;
  1427. ax->d.next = context->aux;
  1428. context->aux = (void *)ax;
  1429. return 0;
  1430. }
  1431. /**
  1432. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1433. * @mqdes: MQ descriptor
  1434. * @u_notification: Notification event
  1435. *
  1436. * Returns 0 for success or NULL context or < 0 on error.
  1437. */
  1438. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1439. {
  1440. struct audit_aux_data_mq_notify *ax;
  1441. struct audit_context *context = current->audit_context;
  1442. if (!audit_enabled)
  1443. return 0;
  1444. if (likely(!context))
  1445. return 0;
  1446. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1447. if (!ax)
  1448. return -ENOMEM;
  1449. if (u_notification != NULL) {
  1450. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1451. kfree(ax);
  1452. return -EFAULT;
  1453. }
  1454. } else
  1455. memset(&ax->notification, 0, sizeof(ax->notification));
  1456. ax->mqdes = mqdes;
  1457. ax->d.type = AUDIT_MQ_NOTIFY;
  1458. ax->d.next = context->aux;
  1459. context->aux = (void *)ax;
  1460. return 0;
  1461. }
  1462. /**
  1463. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1464. * @mqdes: MQ descriptor
  1465. * @mqstat: MQ flags
  1466. *
  1467. * Returns 0 for success or NULL context or < 0 on error.
  1468. */
  1469. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1470. {
  1471. struct audit_aux_data_mq_getsetattr *ax;
  1472. struct audit_context *context = current->audit_context;
  1473. if (!audit_enabled)
  1474. return 0;
  1475. if (likely(!context))
  1476. return 0;
  1477. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1478. if (!ax)
  1479. return -ENOMEM;
  1480. ax->mqdes = mqdes;
  1481. ax->mqstat = *mqstat;
  1482. ax->d.type = AUDIT_MQ_GETSETATTR;
  1483. ax->d.next = context->aux;
  1484. context->aux = (void *)ax;
  1485. return 0;
  1486. }
  1487. /**
  1488. * audit_ipc_obj - record audit data for ipc object
  1489. * @ipcp: ipc permissions
  1490. *
  1491. * Returns 0 for success or NULL context or < 0 on error.
  1492. */
  1493. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1494. {
  1495. struct audit_aux_data_ipcctl *ax;
  1496. struct audit_context *context = current->audit_context;
  1497. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1498. if (!ax)
  1499. return -ENOMEM;
  1500. ax->uid = ipcp->uid;
  1501. ax->gid = ipcp->gid;
  1502. ax->mode = ipcp->mode;
  1503. selinux_get_ipc_sid(ipcp, &ax->osid);
  1504. ax->d.type = AUDIT_IPC;
  1505. ax->d.next = context->aux;
  1506. context->aux = (void *)ax;
  1507. return 0;
  1508. }
  1509. /**
  1510. * audit_ipc_set_perm - record audit data for new ipc permissions
  1511. * @qbytes: msgq bytes
  1512. * @uid: msgq user id
  1513. * @gid: msgq group id
  1514. * @mode: msgq mode (permissions)
  1515. *
  1516. * Returns 0 for success or NULL context or < 0 on error.
  1517. */
  1518. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1519. {
  1520. struct audit_aux_data_ipcctl *ax;
  1521. struct audit_context *context = current->audit_context;
  1522. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1523. if (!ax)
  1524. return -ENOMEM;
  1525. ax->qbytes = qbytes;
  1526. ax->uid = uid;
  1527. ax->gid = gid;
  1528. ax->mode = mode;
  1529. ax->d.type = AUDIT_IPC_SET_PERM;
  1530. ax->d.next = context->aux;
  1531. context->aux = (void *)ax;
  1532. return 0;
  1533. }
  1534. int audit_bprm(struct linux_binprm *bprm)
  1535. {
  1536. struct audit_aux_data_execve *ax;
  1537. struct audit_context *context = current->audit_context;
  1538. unsigned long p, next;
  1539. void *to;
  1540. if (likely(!audit_enabled || !context || context->dummy))
  1541. return 0;
  1542. ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
  1543. GFP_KERNEL);
  1544. if (!ax)
  1545. return -ENOMEM;
  1546. ax->argc = bprm->argc;
  1547. ax->envc = bprm->envc;
  1548. for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
  1549. struct page *page = bprm->page[p / PAGE_SIZE];
  1550. void *kaddr = kmap(page);
  1551. next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
  1552. memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
  1553. to += next - p;
  1554. kunmap(page);
  1555. }
  1556. ax->d.type = AUDIT_EXECVE;
  1557. ax->d.next = context->aux;
  1558. context->aux = (void *)ax;
  1559. return 0;
  1560. }
  1561. /**
  1562. * audit_socketcall - record audit data for sys_socketcall
  1563. * @nargs: number of args
  1564. * @args: args array
  1565. *
  1566. * Returns 0 for success or NULL context or < 0 on error.
  1567. */
  1568. int audit_socketcall(int nargs, unsigned long *args)
  1569. {
  1570. struct audit_aux_data_socketcall *ax;
  1571. struct audit_context *context = current->audit_context;
  1572. if (likely(!context || context->dummy))
  1573. return 0;
  1574. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1575. if (!ax)
  1576. return -ENOMEM;
  1577. ax->nargs = nargs;
  1578. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1579. ax->d.type = AUDIT_SOCKETCALL;
  1580. ax->d.next = context->aux;
  1581. context->aux = (void *)ax;
  1582. return 0;
  1583. }
  1584. /**
  1585. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1586. * @len: data length in user space
  1587. * @a: data address in kernel space
  1588. *
  1589. * Returns 0 for success or NULL context or < 0 on error.
  1590. */
  1591. int audit_sockaddr(int len, void *a)
  1592. {
  1593. struct audit_aux_data_sockaddr *ax;
  1594. struct audit_context *context = current->audit_context;
  1595. if (likely(!context || context->dummy))
  1596. return 0;
  1597. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1598. if (!ax)
  1599. return -ENOMEM;
  1600. ax->len = len;
  1601. memcpy(ax->a, a, len);
  1602. ax->d.type = AUDIT_SOCKADDR;
  1603. ax->d.next = context->aux;
  1604. context->aux = (void *)ax;
  1605. return 0;
  1606. }
  1607. /**
  1608. * audit_avc_path - record the granting or denial of permissions
  1609. * @dentry: dentry to record
  1610. * @mnt: mnt to record
  1611. *
  1612. * Returns 0 for success or NULL context or < 0 on error.
  1613. *
  1614. * Called from security/selinux/avc.c::avc_audit()
  1615. */
  1616. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1617. {
  1618. struct audit_aux_data_path *ax;
  1619. struct audit_context *context = current->audit_context;
  1620. if (likely(!context))
  1621. return 0;
  1622. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1623. if (!ax)
  1624. return -ENOMEM;
  1625. ax->dentry = dget(dentry);
  1626. ax->mnt = mntget(mnt);
  1627. ax->d.type = AUDIT_AVC_PATH;
  1628. ax->d.next = context->aux;
  1629. context->aux = (void *)ax;
  1630. return 0;
  1631. }
  1632. /**
  1633. * audit_signal_info - record signal info for shutting down audit subsystem
  1634. * @sig: signal value
  1635. * @t: task being signaled
  1636. *
  1637. * If the audit subsystem is being terminated, record the task (pid)
  1638. * and uid that is doing that.
  1639. */
  1640. void __audit_signal_info(int sig, struct task_struct *t)
  1641. {
  1642. extern pid_t audit_sig_pid;
  1643. extern uid_t audit_sig_uid;
  1644. extern u32 audit_sig_sid;
  1645. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
  1646. struct task_struct *tsk = current;
  1647. struct audit_context *ctx = tsk->audit_context;
  1648. audit_sig_pid = tsk->pid;
  1649. if (ctx)
  1650. audit_sig_uid = ctx->loginuid;
  1651. else
  1652. audit_sig_uid = tsk->uid;
  1653. selinux_get_task_sid(tsk, &audit_sig_sid);
  1654. }
  1655. }