auditsc.c 48 KB

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