auditsc.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464
  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 <linux/fs.h>
  48. #include <linux/namei.h>
  49. #include <linux/mm.h>
  50. #include <linux/module.h>
  51. #include <linux/mount.h>
  52. #include <linux/socket.h>
  53. #include <linux/mqueue.h>
  54. #include <linux/audit.h>
  55. #include <linux/personality.h>
  56. #include <linux/time.h>
  57. #include <linux/netlink.h>
  58. #include <linux/compiler.h>
  59. #include <asm/unistd.h>
  60. #include <linux/security.h>
  61. #include <linux/list.h>
  62. #include <linux/tty.h>
  63. #include <linux/binfmts.h>
  64. #include <linux/highmem.h>
  65. #include <linux/syscalls.h>
  66. #include <linux/inotify.h>
  67. #include "audit.h"
  68. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  69. * for saving names from getname(). */
  70. #define AUDIT_NAMES 20
  71. /* Indicates that audit should log the full pathname. */
  72. #define AUDIT_NAME_FULL -1
  73. /* no execve audit message should be longer than this (userspace limits) */
  74. #define MAX_EXECVE_AUDIT_LEN 7500
  75. /* number of audit rules */
  76. int audit_n_rules;
  77. /* determines whether we collect data for signals sent */
  78. int audit_signals;
  79. /* When fs/namei.c:getname() is called, we store the pointer in name and
  80. * we don't let putname() free it (instead we free all of the saved
  81. * pointers at syscall exit time).
  82. *
  83. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  84. struct audit_names {
  85. const char *name;
  86. int name_len; /* number of name's characters to log */
  87. unsigned name_put; /* call __putname() for this name */
  88. unsigned long ino;
  89. dev_t dev;
  90. umode_t mode;
  91. uid_t uid;
  92. gid_t gid;
  93. dev_t rdev;
  94. u32 osid;
  95. };
  96. struct audit_aux_data {
  97. struct audit_aux_data *next;
  98. int type;
  99. };
  100. #define AUDIT_AUX_IPCPERM 0
  101. /* Number of target pids per aux struct. */
  102. #define AUDIT_AUX_PIDS 16
  103. struct audit_aux_data_mq_open {
  104. struct audit_aux_data d;
  105. int oflag;
  106. mode_t mode;
  107. struct mq_attr attr;
  108. };
  109. struct audit_aux_data_mq_sendrecv {
  110. struct audit_aux_data d;
  111. mqd_t mqdes;
  112. size_t msg_len;
  113. unsigned int msg_prio;
  114. struct timespec abs_timeout;
  115. };
  116. struct audit_aux_data_mq_notify {
  117. struct audit_aux_data d;
  118. mqd_t mqdes;
  119. struct sigevent notification;
  120. };
  121. struct audit_aux_data_mq_getsetattr {
  122. struct audit_aux_data d;
  123. mqd_t mqdes;
  124. struct mq_attr mqstat;
  125. };
  126. struct audit_aux_data_ipcctl {
  127. struct audit_aux_data d;
  128. struct ipc_perm p;
  129. unsigned long qbytes;
  130. uid_t uid;
  131. gid_t gid;
  132. mode_t mode;
  133. u32 osid;
  134. };
  135. struct audit_aux_data_execve {
  136. struct audit_aux_data d;
  137. int argc;
  138. int envc;
  139. struct mm_struct *mm;
  140. };
  141. struct audit_aux_data_socketcall {
  142. struct audit_aux_data d;
  143. int nargs;
  144. unsigned long args[0];
  145. };
  146. struct audit_aux_data_sockaddr {
  147. struct audit_aux_data d;
  148. int len;
  149. char a[0];
  150. };
  151. struct audit_aux_data_fd_pair {
  152. struct audit_aux_data d;
  153. int fd[2];
  154. };
  155. struct audit_aux_data_pids {
  156. struct audit_aux_data d;
  157. pid_t target_pid[AUDIT_AUX_PIDS];
  158. uid_t target_auid[AUDIT_AUX_PIDS];
  159. uid_t target_uid[AUDIT_AUX_PIDS];
  160. unsigned int target_sessionid[AUDIT_AUX_PIDS];
  161. u32 target_sid[AUDIT_AUX_PIDS];
  162. char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
  163. int pid_count;
  164. };
  165. struct audit_tree_refs {
  166. struct audit_tree_refs *next;
  167. struct audit_chunk *c[31];
  168. };
  169. /* The per-task audit context. */
  170. struct audit_context {
  171. int dummy; /* must be the first element */
  172. int in_syscall; /* 1 if task is in a syscall */
  173. enum audit_state state;
  174. unsigned int serial; /* serial number for record */
  175. struct timespec ctime; /* time of syscall entry */
  176. int major; /* syscall number */
  177. unsigned long argv[4]; /* syscall arguments */
  178. int return_valid; /* return code is valid */
  179. long return_code;/* syscall return code */
  180. int auditable; /* 1 if record should be written */
  181. int name_count;
  182. struct audit_names names[AUDIT_NAMES];
  183. char * filterkey; /* key for rule that triggered record */
  184. struct path pwd;
  185. struct audit_context *previous; /* For nested syscalls */
  186. struct audit_aux_data *aux;
  187. struct audit_aux_data *aux_pids;
  188. /* Save things to print about task_struct */
  189. pid_t pid, ppid;
  190. uid_t uid, euid, suid, fsuid;
  191. gid_t gid, egid, sgid, fsgid;
  192. unsigned long personality;
  193. int arch;
  194. pid_t target_pid;
  195. uid_t target_auid;
  196. uid_t target_uid;
  197. unsigned int target_sessionid;
  198. u32 target_sid;
  199. char target_comm[TASK_COMM_LEN];
  200. struct audit_tree_refs *trees, *first_trees;
  201. int tree_count;
  202. #if AUDIT_DEBUG
  203. int put_count;
  204. int ino_count;
  205. #endif
  206. };
  207. #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
  208. static inline int open_arg(int flags, int mask)
  209. {
  210. int n = ACC_MODE(flags);
  211. if (flags & (O_TRUNC | O_CREAT))
  212. n |= AUDIT_PERM_WRITE;
  213. return n & mask;
  214. }
  215. static int audit_match_perm(struct audit_context *ctx, int mask)
  216. {
  217. unsigned n = ctx->major;
  218. switch (audit_classify_syscall(ctx->arch, n)) {
  219. case 0: /* native */
  220. if ((mask & AUDIT_PERM_WRITE) &&
  221. audit_match_class(AUDIT_CLASS_WRITE, n))
  222. return 1;
  223. if ((mask & AUDIT_PERM_READ) &&
  224. audit_match_class(AUDIT_CLASS_READ, n))
  225. return 1;
  226. if ((mask & AUDIT_PERM_ATTR) &&
  227. audit_match_class(AUDIT_CLASS_CHATTR, n))
  228. return 1;
  229. return 0;
  230. case 1: /* 32bit on biarch */
  231. if ((mask & AUDIT_PERM_WRITE) &&
  232. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  233. return 1;
  234. if ((mask & AUDIT_PERM_READ) &&
  235. audit_match_class(AUDIT_CLASS_READ_32, n))
  236. return 1;
  237. if ((mask & AUDIT_PERM_ATTR) &&
  238. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  239. return 1;
  240. return 0;
  241. case 2: /* open */
  242. return mask & ACC_MODE(ctx->argv[1]);
  243. case 3: /* openat */
  244. return mask & ACC_MODE(ctx->argv[2]);
  245. case 4: /* socketcall */
  246. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  247. case 5: /* execve */
  248. return mask & AUDIT_PERM_EXEC;
  249. default:
  250. return 0;
  251. }
  252. }
  253. static int audit_match_filetype(struct audit_context *ctx, int which)
  254. {
  255. unsigned index = which & ~S_IFMT;
  256. mode_t mode = which & S_IFMT;
  257. if (index >= ctx->name_count)
  258. return 0;
  259. if (ctx->names[index].ino == -1)
  260. return 0;
  261. if ((ctx->names[index].mode ^ mode) & S_IFMT)
  262. return 0;
  263. return 1;
  264. }
  265. /*
  266. * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
  267. * ->first_trees points to its beginning, ->trees - to the current end of data.
  268. * ->tree_count is the number of free entries in array pointed to by ->trees.
  269. * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
  270. * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
  271. * it's going to remain 1-element for almost any setup) until we free context itself.
  272. * References in it _are_ dropped - at the same time we free/drop aux stuff.
  273. */
  274. #ifdef CONFIG_AUDIT_TREE
  275. static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
  276. {
  277. struct audit_tree_refs *p = ctx->trees;
  278. int left = ctx->tree_count;
  279. if (likely(left)) {
  280. p->c[--left] = chunk;
  281. ctx->tree_count = left;
  282. return 1;
  283. }
  284. if (!p)
  285. return 0;
  286. p = p->next;
  287. if (p) {
  288. p->c[30] = chunk;
  289. ctx->trees = p;
  290. ctx->tree_count = 30;
  291. return 1;
  292. }
  293. return 0;
  294. }
  295. static int grow_tree_refs(struct audit_context *ctx)
  296. {
  297. struct audit_tree_refs *p = ctx->trees;
  298. ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
  299. if (!ctx->trees) {
  300. ctx->trees = p;
  301. return 0;
  302. }
  303. if (p)
  304. p->next = ctx->trees;
  305. else
  306. ctx->first_trees = ctx->trees;
  307. ctx->tree_count = 31;
  308. return 1;
  309. }
  310. #endif
  311. static void unroll_tree_refs(struct audit_context *ctx,
  312. struct audit_tree_refs *p, int count)
  313. {
  314. #ifdef CONFIG_AUDIT_TREE
  315. struct audit_tree_refs *q;
  316. int n;
  317. if (!p) {
  318. /* we started with empty chain */
  319. p = ctx->first_trees;
  320. count = 31;
  321. /* if the very first allocation has failed, nothing to do */
  322. if (!p)
  323. return;
  324. }
  325. n = count;
  326. for (q = p; q != ctx->trees; q = q->next, n = 31) {
  327. while (n--) {
  328. audit_put_chunk(q->c[n]);
  329. q->c[n] = NULL;
  330. }
  331. }
  332. while (n-- > ctx->tree_count) {
  333. audit_put_chunk(q->c[n]);
  334. q->c[n] = NULL;
  335. }
  336. ctx->trees = p;
  337. ctx->tree_count = count;
  338. #endif
  339. }
  340. static void free_tree_refs(struct audit_context *ctx)
  341. {
  342. struct audit_tree_refs *p, *q;
  343. for (p = ctx->first_trees; p; p = q) {
  344. q = p->next;
  345. kfree(p);
  346. }
  347. }
  348. static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
  349. {
  350. #ifdef CONFIG_AUDIT_TREE
  351. struct audit_tree_refs *p;
  352. int n;
  353. if (!tree)
  354. return 0;
  355. /* full ones */
  356. for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
  357. for (n = 0; n < 31; n++)
  358. if (audit_tree_match(p->c[n], tree))
  359. return 1;
  360. }
  361. /* partial */
  362. if (p) {
  363. for (n = ctx->tree_count; n < 31; n++)
  364. if (audit_tree_match(p->c[n], tree))
  365. return 1;
  366. }
  367. #endif
  368. return 0;
  369. }
  370. /* Determine if any context name data matches a rule's watch data */
  371. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  372. * otherwise. */
  373. static int audit_filter_rules(struct task_struct *tsk,
  374. struct audit_krule *rule,
  375. struct audit_context *ctx,
  376. struct audit_names *name,
  377. enum audit_state *state)
  378. {
  379. int i, j, need_sid = 1;
  380. u32 sid;
  381. for (i = 0; i < rule->field_count; i++) {
  382. struct audit_field *f = &rule->fields[i];
  383. int result = 0;
  384. switch (f->type) {
  385. case AUDIT_PID:
  386. result = audit_comparator(tsk->pid, f->op, f->val);
  387. break;
  388. case AUDIT_PPID:
  389. if (ctx) {
  390. if (!ctx->ppid)
  391. ctx->ppid = sys_getppid();
  392. result = audit_comparator(ctx->ppid, f->op, f->val);
  393. }
  394. break;
  395. case AUDIT_UID:
  396. result = audit_comparator(tsk->uid, f->op, f->val);
  397. break;
  398. case AUDIT_EUID:
  399. result = audit_comparator(tsk->euid, f->op, f->val);
  400. break;
  401. case AUDIT_SUID:
  402. result = audit_comparator(tsk->suid, f->op, f->val);
  403. break;
  404. case AUDIT_FSUID:
  405. result = audit_comparator(tsk->fsuid, f->op, f->val);
  406. break;
  407. case AUDIT_GID:
  408. result = audit_comparator(tsk->gid, f->op, f->val);
  409. break;
  410. case AUDIT_EGID:
  411. result = audit_comparator(tsk->egid, f->op, f->val);
  412. break;
  413. case AUDIT_SGID:
  414. result = audit_comparator(tsk->sgid, f->op, f->val);
  415. break;
  416. case AUDIT_FSGID:
  417. result = audit_comparator(tsk->fsgid, f->op, f->val);
  418. break;
  419. case AUDIT_PERS:
  420. result = audit_comparator(tsk->personality, f->op, f->val);
  421. break;
  422. case AUDIT_ARCH:
  423. if (ctx)
  424. result = audit_comparator(ctx->arch, f->op, f->val);
  425. break;
  426. case AUDIT_EXIT:
  427. if (ctx && ctx->return_valid)
  428. result = audit_comparator(ctx->return_code, f->op, f->val);
  429. break;
  430. case AUDIT_SUCCESS:
  431. if (ctx && ctx->return_valid) {
  432. if (f->val)
  433. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  434. else
  435. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  436. }
  437. break;
  438. case AUDIT_DEVMAJOR:
  439. if (name)
  440. result = audit_comparator(MAJOR(name->dev),
  441. f->op, f->val);
  442. else if (ctx) {
  443. for (j = 0; j < ctx->name_count; j++) {
  444. if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
  445. ++result;
  446. break;
  447. }
  448. }
  449. }
  450. break;
  451. case AUDIT_DEVMINOR:
  452. if (name)
  453. result = audit_comparator(MINOR(name->dev),
  454. f->op, f->val);
  455. else if (ctx) {
  456. for (j = 0; j < ctx->name_count; j++) {
  457. if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
  458. ++result;
  459. break;
  460. }
  461. }
  462. }
  463. break;
  464. case AUDIT_INODE:
  465. if (name)
  466. result = (name->ino == f->val);
  467. else if (ctx) {
  468. for (j = 0; j < ctx->name_count; j++) {
  469. if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
  470. ++result;
  471. break;
  472. }
  473. }
  474. }
  475. break;
  476. case AUDIT_WATCH:
  477. if (name && rule->watch->ino != (unsigned long)-1)
  478. result = (name->dev == rule->watch->dev &&
  479. name->ino == rule->watch->ino);
  480. break;
  481. case AUDIT_DIR:
  482. if (ctx)
  483. result = match_tree_refs(ctx, rule->tree);
  484. break;
  485. case AUDIT_LOGINUID:
  486. result = 0;
  487. if (ctx)
  488. result = audit_comparator(tsk->loginuid, f->op, f->val);
  489. break;
  490. case AUDIT_SUBJ_USER:
  491. case AUDIT_SUBJ_ROLE:
  492. case AUDIT_SUBJ_TYPE:
  493. case AUDIT_SUBJ_SEN:
  494. case AUDIT_SUBJ_CLR:
  495. /* NOTE: this may return negative values indicating
  496. a temporary error. We simply treat this as a
  497. match for now to avoid losing information that
  498. may be wanted. An error message will also be
  499. logged upon error */
  500. if (f->lsm_rule) {
  501. if (need_sid) {
  502. security_task_getsecid(tsk, &sid);
  503. need_sid = 0;
  504. }
  505. result = security_audit_rule_match(sid, f->type,
  506. f->op,
  507. f->lsm_rule,
  508. ctx);
  509. }
  510. break;
  511. case AUDIT_OBJ_USER:
  512. case AUDIT_OBJ_ROLE:
  513. case AUDIT_OBJ_TYPE:
  514. case AUDIT_OBJ_LEV_LOW:
  515. case AUDIT_OBJ_LEV_HIGH:
  516. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  517. also applies here */
  518. if (f->lsm_rule) {
  519. /* Find files that match */
  520. if (name) {
  521. result = security_audit_rule_match(
  522. name->osid, f->type, f->op,
  523. f->lsm_rule, ctx);
  524. } else if (ctx) {
  525. for (j = 0; j < ctx->name_count; j++) {
  526. if (security_audit_rule_match(
  527. ctx->names[j].osid,
  528. f->type, f->op,
  529. f->lsm_rule, ctx)) {
  530. ++result;
  531. break;
  532. }
  533. }
  534. }
  535. /* Find ipc objects that match */
  536. if (ctx) {
  537. struct audit_aux_data *aux;
  538. for (aux = ctx->aux; aux;
  539. aux = aux->next) {
  540. if (aux->type == AUDIT_IPC) {
  541. struct audit_aux_data_ipcctl *axi = (void *)aux;
  542. if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) {
  543. ++result;
  544. break;
  545. }
  546. }
  547. }
  548. }
  549. }
  550. break;
  551. case AUDIT_ARG0:
  552. case AUDIT_ARG1:
  553. case AUDIT_ARG2:
  554. case AUDIT_ARG3:
  555. if (ctx)
  556. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  557. break;
  558. case AUDIT_FILTERKEY:
  559. /* ignore this field for filtering */
  560. result = 1;
  561. break;
  562. case AUDIT_PERM:
  563. result = audit_match_perm(ctx, f->val);
  564. break;
  565. case AUDIT_FILETYPE:
  566. result = audit_match_filetype(ctx, f->val);
  567. break;
  568. }
  569. if (!result)
  570. return 0;
  571. }
  572. if (rule->filterkey)
  573. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  574. switch (rule->action) {
  575. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  576. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  577. }
  578. return 1;
  579. }
  580. /* At process creation time, we can determine if system-call auditing is
  581. * completely disabled for this task. Since we only have the task
  582. * structure at this point, we can only check uid and gid.
  583. */
  584. static enum audit_state audit_filter_task(struct task_struct *tsk)
  585. {
  586. struct audit_entry *e;
  587. enum audit_state state;
  588. rcu_read_lock();
  589. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  590. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  591. rcu_read_unlock();
  592. return state;
  593. }
  594. }
  595. rcu_read_unlock();
  596. return AUDIT_BUILD_CONTEXT;
  597. }
  598. /* At syscall entry and exit time, this filter is called if the
  599. * audit_state is not low enough that auditing cannot take place, but is
  600. * also not high enough that we already know we have to write an audit
  601. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  602. */
  603. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  604. struct audit_context *ctx,
  605. struct list_head *list)
  606. {
  607. struct audit_entry *e;
  608. enum audit_state state;
  609. if (audit_pid && tsk->tgid == audit_pid)
  610. return AUDIT_DISABLED;
  611. rcu_read_lock();
  612. if (!list_empty(list)) {
  613. int word = AUDIT_WORD(ctx->major);
  614. int bit = AUDIT_BIT(ctx->major);
  615. list_for_each_entry_rcu(e, list, list) {
  616. if ((e->rule.mask[word] & bit) == bit &&
  617. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  618. &state)) {
  619. rcu_read_unlock();
  620. return state;
  621. }
  622. }
  623. }
  624. rcu_read_unlock();
  625. return AUDIT_BUILD_CONTEXT;
  626. }
  627. /* At syscall exit time, this filter is called if any audit_names[] have been
  628. * collected during syscall processing. We only check rules in sublists at hash
  629. * buckets applicable to the inode numbers in audit_names[].
  630. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  631. */
  632. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  633. struct audit_context *ctx)
  634. {
  635. int i;
  636. struct audit_entry *e;
  637. enum audit_state state;
  638. if (audit_pid && tsk->tgid == audit_pid)
  639. return AUDIT_DISABLED;
  640. rcu_read_lock();
  641. for (i = 0; i < ctx->name_count; i++) {
  642. int word = AUDIT_WORD(ctx->major);
  643. int bit = AUDIT_BIT(ctx->major);
  644. struct audit_names *n = &ctx->names[i];
  645. int h = audit_hash_ino((u32)n->ino);
  646. struct list_head *list = &audit_inode_hash[h];
  647. if (list_empty(list))
  648. continue;
  649. list_for_each_entry_rcu(e, list, list) {
  650. if ((e->rule.mask[word] & bit) == bit &&
  651. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  652. rcu_read_unlock();
  653. return state;
  654. }
  655. }
  656. }
  657. rcu_read_unlock();
  658. return AUDIT_BUILD_CONTEXT;
  659. }
  660. void audit_set_auditable(struct audit_context *ctx)
  661. {
  662. ctx->auditable = 1;
  663. }
  664. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  665. int return_valid,
  666. int return_code)
  667. {
  668. struct audit_context *context = tsk->audit_context;
  669. if (likely(!context))
  670. return NULL;
  671. context->return_valid = return_valid;
  672. /*
  673. * we need to fix up the return code in the audit logs if the actual
  674. * return codes are later going to be fixed up by the arch specific
  675. * signal handlers
  676. *
  677. * This is actually a test for:
  678. * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
  679. * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
  680. *
  681. * but is faster than a bunch of ||
  682. */
  683. if (unlikely(return_code <= -ERESTARTSYS) &&
  684. (return_code >= -ERESTART_RESTARTBLOCK) &&
  685. (return_code != -ENOIOCTLCMD))
  686. context->return_code = -EINTR;
  687. else
  688. context->return_code = return_code;
  689. if (context->in_syscall && !context->dummy && !context->auditable) {
  690. enum audit_state state;
  691. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  692. if (state == AUDIT_RECORD_CONTEXT) {
  693. context->auditable = 1;
  694. goto get_context;
  695. }
  696. state = audit_filter_inodes(tsk, context);
  697. if (state == AUDIT_RECORD_CONTEXT)
  698. context->auditable = 1;
  699. }
  700. get_context:
  701. tsk->audit_context = NULL;
  702. return context;
  703. }
  704. static inline void audit_free_names(struct audit_context *context)
  705. {
  706. int i;
  707. #if AUDIT_DEBUG == 2
  708. if (context->auditable
  709. ||context->put_count + context->ino_count != context->name_count) {
  710. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  711. " name_count=%d put_count=%d"
  712. " ino_count=%d [NOT freeing]\n",
  713. __FILE__, __LINE__,
  714. context->serial, context->major, context->in_syscall,
  715. context->name_count, context->put_count,
  716. context->ino_count);
  717. for (i = 0; i < context->name_count; i++) {
  718. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  719. context->names[i].name,
  720. context->names[i].name ?: "(null)");
  721. }
  722. dump_stack();
  723. return;
  724. }
  725. #endif
  726. #if AUDIT_DEBUG
  727. context->put_count = 0;
  728. context->ino_count = 0;
  729. #endif
  730. for (i = 0; i < context->name_count; i++) {
  731. if (context->names[i].name && context->names[i].name_put)
  732. __putname(context->names[i].name);
  733. }
  734. context->name_count = 0;
  735. path_put(&context->pwd);
  736. context->pwd.dentry = NULL;
  737. context->pwd.mnt = NULL;
  738. }
  739. static inline void audit_free_aux(struct audit_context *context)
  740. {
  741. struct audit_aux_data *aux;
  742. while ((aux = context->aux)) {
  743. context->aux = aux->next;
  744. kfree(aux);
  745. }
  746. while ((aux = context->aux_pids)) {
  747. context->aux_pids = aux->next;
  748. kfree(aux);
  749. }
  750. }
  751. static inline void audit_zero_context(struct audit_context *context,
  752. enum audit_state state)
  753. {
  754. memset(context, 0, sizeof(*context));
  755. context->state = state;
  756. }
  757. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  758. {
  759. struct audit_context *context;
  760. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  761. return NULL;
  762. audit_zero_context(context, state);
  763. return context;
  764. }
  765. /**
  766. * audit_alloc - allocate an audit context block for a task
  767. * @tsk: task
  768. *
  769. * Filter on the task information and allocate a per-task audit context
  770. * if necessary. Doing so turns on system call auditing for the
  771. * specified task. This is called from copy_process, so no lock is
  772. * needed.
  773. */
  774. int audit_alloc(struct task_struct *tsk)
  775. {
  776. struct audit_context *context;
  777. enum audit_state state;
  778. if (likely(!audit_ever_enabled))
  779. return 0; /* Return if not auditing. */
  780. state = audit_filter_task(tsk);
  781. if (likely(state == AUDIT_DISABLED))
  782. return 0;
  783. if (!(context = audit_alloc_context(state))) {
  784. audit_log_lost("out of memory in audit_alloc");
  785. return -ENOMEM;
  786. }
  787. tsk->audit_context = context;
  788. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  789. return 0;
  790. }
  791. static inline void audit_free_context(struct audit_context *context)
  792. {
  793. struct audit_context *previous;
  794. int count = 0;
  795. do {
  796. previous = context->previous;
  797. if (previous || (count && count < 10)) {
  798. ++count;
  799. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  800. " freeing multiple contexts (%d)\n",
  801. context->serial, context->major,
  802. context->name_count, count);
  803. }
  804. audit_free_names(context);
  805. unroll_tree_refs(context, NULL, 0);
  806. free_tree_refs(context);
  807. audit_free_aux(context);
  808. kfree(context->filterkey);
  809. kfree(context);
  810. context = previous;
  811. } while (context);
  812. if (count >= 10)
  813. printk(KERN_ERR "audit: freed %d contexts\n", count);
  814. }
  815. void audit_log_task_context(struct audit_buffer *ab)
  816. {
  817. char *ctx = NULL;
  818. unsigned len;
  819. int error;
  820. u32 sid;
  821. security_task_getsecid(current, &sid);
  822. if (!sid)
  823. return;
  824. error = security_secid_to_secctx(sid, &ctx, &len);
  825. if (error) {
  826. if (error != -EINVAL)
  827. goto error_path;
  828. return;
  829. }
  830. audit_log_format(ab, " subj=%s", ctx);
  831. security_release_secctx(ctx, len);
  832. return;
  833. error_path:
  834. audit_panic("error in audit_log_task_context");
  835. return;
  836. }
  837. EXPORT_SYMBOL(audit_log_task_context);
  838. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  839. {
  840. char name[sizeof(tsk->comm)];
  841. struct mm_struct *mm = tsk->mm;
  842. struct vm_area_struct *vma;
  843. /* tsk == current */
  844. get_task_comm(name, tsk);
  845. audit_log_format(ab, " comm=");
  846. audit_log_untrustedstring(ab, name);
  847. if (mm) {
  848. down_read(&mm->mmap_sem);
  849. vma = mm->mmap;
  850. while (vma) {
  851. if ((vma->vm_flags & VM_EXECUTABLE) &&
  852. vma->vm_file) {
  853. audit_log_d_path(ab, "exe=",
  854. &vma->vm_file->f_path);
  855. break;
  856. }
  857. vma = vma->vm_next;
  858. }
  859. up_read(&mm->mmap_sem);
  860. }
  861. audit_log_task_context(ab);
  862. }
  863. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  864. uid_t auid, uid_t uid, unsigned int sessionid,
  865. u32 sid, char *comm)
  866. {
  867. struct audit_buffer *ab;
  868. char *ctx = NULL;
  869. u32 len;
  870. int rc = 0;
  871. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  872. if (!ab)
  873. return rc;
  874. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
  875. uid, sessionid);
  876. if (security_secid_to_secctx(sid, &ctx, &len)) {
  877. audit_log_format(ab, " obj=(none)");
  878. rc = 1;
  879. } else {
  880. audit_log_format(ab, " obj=%s", ctx);
  881. security_release_secctx(ctx, len);
  882. }
  883. audit_log_format(ab, " ocomm=");
  884. audit_log_untrustedstring(ab, comm);
  885. audit_log_end(ab);
  886. return rc;
  887. }
  888. /*
  889. * to_send and len_sent accounting are very loose estimates. We aren't
  890. * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
  891. * within about 500 bytes (next page boundry)
  892. *
  893. * why snprintf? an int is up to 12 digits long. if we just assumed when
  894. * logging that a[%d]= was going to be 16 characters long we would be wasting
  895. * space in every audit message. In one 7500 byte message we can log up to
  896. * about 1000 min size arguments. That comes down to about 50% waste of space
  897. * if we didn't do the snprintf to find out how long arg_num_len was.
  898. */
  899. static int audit_log_single_execve_arg(struct audit_context *context,
  900. struct audit_buffer **ab,
  901. int arg_num,
  902. size_t *len_sent,
  903. const char __user *p,
  904. char *buf)
  905. {
  906. char arg_num_len_buf[12];
  907. const char __user *tmp_p = p;
  908. /* how many digits are in arg_num? 3 is the length of a=\n */
  909. size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3;
  910. size_t len, len_left, to_send;
  911. size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
  912. unsigned int i, has_cntl = 0, too_long = 0;
  913. int ret;
  914. /* strnlen_user includes the null we don't want to send */
  915. len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  916. /*
  917. * We just created this mm, if we can't find the strings
  918. * we just copied into it something is _very_ wrong. Similar
  919. * for strings that are too long, we should not have created
  920. * any.
  921. */
  922. if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
  923. WARN_ON(1);
  924. send_sig(SIGKILL, current, 0);
  925. return -1;
  926. }
  927. /* walk the whole argument looking for non-ascii chars */
  928. do {
  929. if (len_left > MAX_EXECVE_AUDIT_LEN)
  930. to_send = MAX_EXECVE_AUDIT_LEN;
  931. else
  932. to_send = len_left;
  933. ret = copy_from_user(buf, tmp_p, to_send);
  934. /*
  935. * There is no reason for this copy to be short. We just
  936. * copied them here, and the mm hasn't been exposed to user-
  937. * space yet.
  938. */
  939. if (ret) {
  940. WARN_ON(1);
  941. send_sig(SIGKILL, current, 0);
  942. return -1;
  943. }
  944. buf[to_send] = '\0';
  945. has_cntl = audit_string_contains_control(buf, to_send);
  946. if (has_cntl) {
  947. /*
  948. * hex messages get logged as 2 bytes, so we can only
  949. * send half as much in each message
  950. */
  951. max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
  952. break;
  953. }
  954. len_left -= to_send;
  955. tmp_p += to_send;
  956. } while (len_left > 0);
  957. len_left = len;
  958. if (len > max_execve_audit_len)
  959. too_long = 1;
  960. /* rewalk the argument actually logging the message */
  961. for (i = 0; len_left > 0; i++) {
  962. int room_left;
  963. if (len_left > max_execve_audit_len)
  964. to_send = max_execve_audit_len;
  965. else
  966. to_send = len_left;
  967. /* do we have space left to send this argument in this ab? */
  968. room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
  969. if (has_cntl)
  970. room_left -= (to_send * 2);
  971. else
  972. room_left -= to_send;
  973. if (room_left < 0) {
  974. *len_sent = 0;
  975. audit_log_end(*ab);
  976. *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
  977. if (!*ab)
  978. return 0;
  979. }
  980. /*
  981. * first record needs to say how long the original string was
  982. * so we can be sure nothing was lost.
  983. */
  984. if ((i == 0) && (too_long))
  985. audit_log_format(*ab, "a%d_len=%zu ", arg_num,
  986. has_cntl ? 2*len : len);
  987. /*
  988. * normally arguments are small enough to fit and we already
  989. * filled buf above when we checked for control characters
  990. * so don't bother with another copy_from_user
  991. */
  992. if (len >= max_execve_audit_len)
  993. ret = copy_from_user(buf, p, to_send);
  994. else
  995. ret = 0;
  996. if (ret) {
  997. WARN_ON(1);
  998. send_sig(SIGKILL, current, 0);
  999. return -1;
  1000. }
  1001. buf[to_send] = '\0';
  1002. /* actually log it */
  1003. audit_log_format(*ab, "a%d", arg_num);
  1004. if (too_long)
  1005. audit_log_format(*ab, "[%d]", i);
  1006. audit_log_format(*ab, "=");
  1007. if (has_cntl)
  1008. audit_log_n_hex(*ab, buf, to_send);
  1009. else
  1010. audit_log_format(*ab, "\"%s\"", buf);
  1011. audit_log_format(*ab, "\n");
  1012. p += to_send;
  1013. len_left -= to_send;
  1014. *len_sent += arg_num_len;
  1015. if (has_cntl)
  1016. *len_sent += to_send * 2;
  1017. else
  1018. *len_sent += to_send;
  1019. }
  1020. /* include the null we didn't log */
  1021. return len + 1;
  1022. }
  1023. static void audit_log_execve_info(struct audit_context *context,
  1024. struct audit_buffer **ab,
  1025. struct audit_aux_data_execve *axi)
  1026. {
  1027. int i;
  1028. size_t len, len_sent = 0;
  1029. const char __user *p;
  1030. char *buf;
  1031. if (axi->mm != current->mm)
  1032. return; /* execve failed, no additional info */
  1033. p = (const char __user *)axi->mm->arg_start;
  1034. audit_log_format(*ab, "argc=%d ", axi->argc);
  1035. /*
  1036. * we need some kernel buffer to hold the userspace args. Just
  1037. * allocate one big one rather than allocating one of the right size
  1038. * for every single argument inside audit_log_single_execve_arg()
  1039. * should be <8k allocation so should be pretty safe.
  1040. */
  1041. buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1042. if (!buf) {
  1043. audit_panic("out of memory for argv string\n");
  1044. return;
  1045. }
  1046. for (i = 0; i < axi->argc; i++) {
  1047. len = audit_log_single_execve_arg(context, ab, i,
  1048. &len_sent, p, buf);
  1049. if (len <= 0)
  1050. break;
  1051. p += len;
  1052. }
  1053. kfree(buf);
  1054. }
  1055. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  1056. {
  1057. int i, call_panic = 0;
  1058. struct audit_buffer *ab;
  1059. struct audit_aux_data *aux;
  1060. const char *tty;
  1061. /* tsk == current */
  1062. context->pid = tsk->pid;
  1063. if (!context->ppid)
  1064. context->ppid = sys_getppid();
  1065. context->uid = tsk->uid;
  1066. context->gid = tsk->gid;
  1067. context->euid = tsk->euid;
  1068. context->suid = tsk->suid;
  1069. context->fsuid = tsk->fsuid;
  1070. context->egid = tsk->egid;
  1071. context->sgid = tsk->sgid;
  1072. context->fsgid = tsk->fsgid;
  1073. context->personality = tsk->personality;
  1074. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1075. if (!ab)
  1076. return; /* audit_panic has been called */
  1077. audit_log_format(ab, "arch=%x syscall=%d",
  1078. context->arch, context->major);
  1079. if (context->personality != PER_LINUX)
  1080. audit_log_format(ab, " per=%lx", context->personality);
  1081. if (context->return_valid)
  1082. audit_log_format(ab, " success=%s exit=%ld",
  1083. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  1084. context->return_code);
  1085. mutex_lock(&tty_mutex);
  1086. read_lock(&tasklist_lock);
  1087. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  1088. tty = tsk->signal->tty->name;
  1089. else
  1090. tty = "(none)";
  1091. read_unlock(&tasklist_lock);
  1092. audit_log_format(ab,
  1093. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  1094. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  1095. " euid=%u suid=%u fsuid=%u"
  1096. " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
  1097. context->argv[0],
  1098. context->argv[1],
  1099. context->argv[2],
  1100. context->argv[3],
  1101. context->name_count,
  1102. context->ppid,
  1103. context->pid,
  1104. tsk->loginuid,
  1105. context->uid,
  1106. context->gid,
  1107. context->euid, context->suid, context->fsuid,
  1108. context->egid, context->sgid, context->fsgid, tty,
  1109. tsk->sessionid);
  1110. mutex_unlock(&tty_mutex);
  1111. audit_log_task_info(ab, tsk);
  1112. if (context->filterkey) {
  1113. audit_log_format(ab, " key=");
  1114. audit_log_untrustedstring(ab, context->filterkey);
  1115. } else
  1116. audit_log_format(ab, " key=(null)");
  1117. audit_log_end(ab);
  1118. for (aux = context->aux; aux; aux = aux->next) {
  1119. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1120. if (!ab)
  1121. continue; /* audit_panic has been called */
  1122. switch (aux->type) {
  1123. case AUDIT_MQ_OPEN: {
  1124. struct audit_aux_data_mq_open *axi = (void *)aux;
  1125. audit_log_format(ab,
  1126. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  1127. "mq_msgsize=%ld mq_curmsgs=%ld",
  1128. axi->oflag, axi->mode, axi->attr.mq_flags,
  1129. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  1130. axi->attr.mq_curmsgs);
  1131. break; }
  1132. case AUDIT_MQ_SENDRECV: {
  1133. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  1134. audit_log_format(ab,
  1135. "mqdes=%d msg_len=%zd msg_prio=%u "
  1136. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  1137. axi->mqdes, axi->msg_len, axi->msg_prio,
  1138. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  1139. break; }
  1140. case AUDIT_MQ_NOTIFY: {
  1141. struct audit_aux_data_mq_notify *axi = (void *)aux;
  1142. audit_log_format(ab,
  1143. "mqdes=%d sigev_signo=%d",
  1144. axi->mqdes,
  1145. axi->notification.sigev_signo);
  1146. break; }
  1147. case AUDIT_MQ_GETSETATTR: {
  1148. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  1149. audit_log_format(ab,
  1150. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1151. "mq_curmsgs=%ld ",
  1152. axi->mqdes,
  1153. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  1154. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  1155. break; }
  1156. case AUDIT_IPC: {
  1157. struct audit_aux_data_ipcctl *axi = (void *)aux;
  1158. audit_log_format(ab,
  1159. "ouid=%u ogid=%u mode=%#o",
  1160. axi->uid, axi->gid, axi->mode);
  1161. if (axi->osid != 0) {
  1162. char *ctx = NULL;
  1163. u32 len;
  1164. if (security_secid_to_secctx(
  1165. axi->osid, &ctx, &len)) {
  1166. audit_log_format(ab, " osid=%u",
  1167. axi->osid);
  1168. call_panic = 1;
  1169. } else {
  1170. audit_log_format(ab, " obj=%s", ctx);
  1171. security_release_secctx(ctx, len);
  1172. }
  1173. }
  1174. break; }
  1175. case AUDIT_IPC_SET_PERM: {
  1176. struct audit_aux_data_ipcctl *axi = (void *)aux;
  1177. audit_log_format(ab,
  1178. "qbytes=%lx ouid=%u ogid=%u mode=%#o",
  1179. axi->qbytes, axi->uid, axi->gid, axi->mode);
  1180. break; }
  1181. case AUDIT_EXECVE: {
  1182. struct audit_aux_data_execve *axi = (void *)aux;
  1183. audit_log_execve_info(context, &ab, axi);
  1184. break; }
  1185. case AUDIT_SOCKETCALL: {
  1186. struct audit_aux_data_socketcall *axs = (void *)aux;
  1187. audit_log_format(ab, "nargs=%d", axs->nargs);
  1188. for (i=0; i<axs->nargs; i++)
  1189. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  1190. break; }
  1191. case AUDIT_SOCKADDR: {
  1192. struct audit_aux_data_sockaddr *axs = (void *)aux;
  1193. audit_log_format(ab, "saddr=");
  1194. audit_log_n_hex(ab, axs->a, axs->len);
  1195. break; }
  1196. case AUDIT_FD_PAIR: {
  1197. struct audit_aux_data_fd_pair *axs = (void *)aux;
  1198. audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
  1199. break; }
  1200. }
  1201. audit_log_end(ab);
  1202. }
  1203. for (aux = context->aux_pids; aux; aux = aux->next) {
  1204. struct audit_aux_data_pids *axs = (void *)aux;
  1205. for (i = 0; i < axs->pid_count; i++)
  1206. if (audit_log_pid_context(context, axs->target_pid[i],
  1207. axs->target_auid[i],
  1208. axs->target_uid[i],
  1209. axs->target_sessionid[i],
  1210. axs->target_sid[i],
  1211. axs->target_comm[i]))
  1212. call_panic = 1;
  1213. }
  1214. if (context->target_pid &&
  1215. audit_log_pid_context(context, context->target_pid,
  1216. context->target_auid, context->target_uid,
  1217. context->target_sessionid,
  1218. context->target_sid, context->target_comm))
  1219. call_panic = 1;
  1220. if (context->pwd.dentry && context->pwd.mnt) {
  1221. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1222. if (ab) {
  1223. audit_log_d_path(ab, "cwd=", &context->pwd);
  1224. audit_log_end(ab);
  1225. }
  1226. }
  1227. for (i = 0; i < context->name_count; i++) {
  1228. struct audit_names *n = &context->names[i];
  1229. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  1230. if (!ab)
  1231. continue; /* audit_panic has been called */
  1232. audit_log_format(ab, "item=%d", i);
  1233. if (n->name) {
  1234. switch(n->name_len) {
  1235. case AUDIT_NAME_FULL:
  1236. /* log the full path */
  1237. audit_log_format(ab, " name=");
  1238. audit_log_untrustedstring(ab, n->name);
  1239. break;
  1240. case 0:
  1241. /* name was specified as a relative path and the
  1242. * directory component is the cwd */
  1243. audit_log_d_path(ab, " name=", &context->pwd);
  1244. break;
  1245. default:
  1246. /* log the name's directory component */
  1247. audit_log_format(ab, " name=");
  1248. audit_log_n_untrustedstring(ab, n->name,
  1249. n->name_len);
  1250. }
  1251. } else
  1252. audit_log_format(ab, " name=(null)");
  1253. if (n->ino != (unsigned long)-1) {
  1254. audit_log_format(ab, " inode=%lu"
  1255. " dev=%02x:%02x mode=%#o"
  1256. " ouid=%u ogid=%u rdev=%02x:%02x",
  1257. n->ino,
  1258. MAJOR(n->dev),
  1259. MINOR(n->dev),
  1260. n->mode,
  1261. n->uid,
  1262. n->gid,
  1263. MAJOR(n->rdev),
  1264. MINOR(n->rdev));
  1265. }
  1266. if (n->osid != 0) {
  1267. char *ctx = NULL;
  1268. u32 len;
  1269. if (security_secid_to_secctx(
  1270. n->osid, &ctx, &len)) {
  1271. audit_log_format(ab, " osid=%u", n->osid);
  1272. call_panic = 2;
  1273. } else {
  1274. audit_log_format(ab, " obj=%s", ctx);
  1275. security_release_secctx(ctx, len);
  1276. }
  1277. }
  1278. audit_log_end(ab);
  1279. }
  1280. /* Send end of event record to help user space know we are finished */
  1281. ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
  1282. if (ab)
  1283. audit_log_end(ab);
  1284. if (call_panic)
  1285. audit_panic("error converting sid to string");
  1286. }
  1287. /**
  1288. * audit_free - free a per-task audit context
  1289. * @tsk: task whose audit context block to free
  1290. *
  1291. * Called from copy_process and do_exit
  1292. */
  1293. void audit_free(struct task_struct *tsk)
  1294. {
  1295. struct audit_context *context;
  1296. context = audit_get_context(tsk, 0, 0);
  1297. if (likely(!context))
  1298. return;
  1299. /* Check for system calls that do not go through the exit
  1300. * function (e.g., exit_group), then free context block.
  1301. * We use GFP_ATOMIC here because we might be doing this
  1302. * in the context of the idle thread */
  1303. /* that can happen only if we are called from do_exit() */
  1304. if (context->in_syscall && context->auditable)
  1305. audit_log_exit(context, tsk);
  1306. audit_free_context(context);
  1307. }
  1308. /**
  1309. * audit_syscall_entry - fill in an audit record at syscall entry
  1310. * @tsk: task being audited
  1311. * @arch: architecture type
  1312. * @major: major syscall type (function)
  1313. * @a1: additional syscall register 1
  1314. * @a2: additional syscall register 2
  1315. * @a3: additional syscall register 3
  1316. * @a4: additional syscall register 4
  1317. *
  1318. * Fill in audit context at syscall entry. This only happens if the
  1319. * audit context was created when the task was created and the state or
  1320. * filters demand the audit context be built. If the state from the
  1321. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1322. * then the record will be written at syscall exit time (otherwise, it
  1323. * will only be written if another part of the kernel requests that it
  1324. * be written).
  1325. */
  1326. void audit_syscall_entry(int arch, int major,
  1327. unsigned long a1, unsigned long a2,
  1328. unsigned long a3, unsigned long a4)
  1329. {
  1330. struct task_struct *tsk = current;
  1331. struct audit_context *context = tsk->audit_context;
  1332. enum audit_state state;
  1333. if (unlikely(!context))
  1334. return;
  1335. /*
  1336. * This happens only on certain architectures that make system
  1337. * calls in kernel_thread via the entry.S interface, instead of
  1338. * with direct calls. (If you are porting to a new
  1339. * architecture, hitting this condition can indicate that you
  1340. * got the _exit/_leave calls backward in entry.S.)
  1341. *
  1342. * i386 no
  1343. * x86_64 no
  1344. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  1345. *
  1346. * This also happens with vm86 emulation in a non-nested manner
  1347. * (entries without exits), so this case must be caught.
  1348. */
  1349. if (context->in_syscall) {
  1350. struct audit_context *newctx;
  1351. #if AUDIT_DEBUG
  1352. printk(KERN_ERR
  1353. "audit(:%d) pid=%d in syscall=%d;"
  1354. " entering syscall=%d\n",
  1355. context->serial, tsk->pid, context->major, major);
  1356. #endif
  1357. newctx = audit_alloc_context(context->state);
  1358. if (newctx) {
  1359. newctx->previous = context;
  1360. context = newctx;
  1361. tsk->audit_context = newctx;
  1362. } else {
  1363. /* If we can't alloc a new context, the best we
  1364. * can do is to leak memory (any pending putname
  1365. * will be lost). The only other alternative is
  1366. * to abandon auditing. */
  1367. audit_zero_context(context, context->state);
  1368. }
  1369. }
  1370. BUG_ON(context->in_syscall || context->name_count);
  1371. if (!audit_enabled)
  1372. return;
  1373. context->arch = arch;
  1374. context->major = major;
  1375. context->argv[0] = a1;
  1376. context->argv[1] = a2;
  1377. context->argv[2] = a3;
  1378. context->argv[3] = a4;
  1379. state = context->state;
  1380. context->dummy = !audit_n_rules;
  1381. if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
  1382. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1383. if (likely(state == AUDIT_DISABLED))
  1384. return;
  1385. context->serial = 0;
  1386. context->ctime = CURRENT_TIME;
  1387. context->in_syscall = 1;
  1388. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  1389. context->ppid = 0;
  1390. }
  1391. /**
  1392. * audit_syscall_exit - deallocate audit context after a system call
  1393. * @tsk: task being audited
  1394. * @valid: success/failure flag
  1395. * @return_code: syscall return value
  1396. *
  1397. * Tear down after system call. If the audit context has been marked as
  1398. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1399. * filtering, or because some other part of the kernel write an audit
  1400. * message), then write out the syscall information. In call cases,
  1401. * free the names stored from getname().
  1402. */
  1403. void audit_syscall_exit(int valid, long return_code)
  1404. {
  1405. struct task_struct *tsk = current;
  1406. struct audit_context *context;
  1407. context = audit_get_context(tsk, valid, return_code);
  1408. if (likely(!context))
  1409. return;
  1410. if (context->in_syscall && context->auditable)
  1411. audit_log_exit(context, tsk);
  1412. context->in_syscall = 0;
  1413. context->auditable = 0;
  1414. if (context->previous) {
  1415. struct audit_context *new_context = context->previous;
  1416. context->previous = NULL;
  1417. audit_free_context(context);
  1418. tsk->audit_context = new_context;
  1419. } else {
  1420. audit_free_names(context);
  1421. unroll_tree_refs(context, NULL, 0);
  1422. audit_free_aux(context);
  1423. context->aux = NULL;
  1424. context->aux_pids = NULL;
  1425. context->target_pid = 0;
  1426. context->target_sid = 0;
  1427. kfree(context->filterkey);
  1428. context->filterkey = NULL;
  1429. tsk->audit_context = context;
  1430. }
  1431. }
  1432. static inline void handle_one(const struct inode *inode)
  1433. {
  1434. #ifdef CONFIG_AUDIT_TREE
  1435. struct audit_context *context;
  1436. struct audit_tree_refs *p;
  1437. struct audit_chunk *chunk;
  1438. int count;
  1439. if (likely(list_empty(&inode->inotify_watches)))
  1440. return;
  1441. context = current->audit_context;
  1442. p = context->trees;
  1443. count = context->tree_count;
  1444. rcu_read_lock();
  1445. chunk = audit_tree_lookup(inode);
  1446. rcu_read_unlock();
  1447. if (!chunk)
  1448. return;
  1449. if (likely(put_tree_ref(context, chunk)))
  1450. return;
  1451. if (unlikely(!grow_tree_refs(context))) {
  1452. printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
  1453. audit_set_auditable(context);
  1454. audit_put_chunk(chunk);
  1455. unroll_tree_refs(context, p, count);
  1456. return;
  1457. }
  1458. put_tree_ref(context, chunk);
  1459. #endif
  1460. }
  1461. static void handle_path(const struct dentry *dentry)
  1462. {
  1463. #ifdef CONFIG_AUDIT_TREE
  1464. struct audit_context *context;
  1465. struct audit_tree_refs *p;
  1466. const struct dentry *d, *parent;
  1467. struct audit_chunk *drop;
  1468. unsigned long seq;
  1469. int count;
  1470. context = current->audit_context;
  1471. p = context->trees;
  1472. count = context->tree_count;
  1473. retry:
  1474. drop = NULL;
  1475. d = dentry;
  1476. rcu_read_lock();
  1477. seq = read_seqbegin(&rename_lock);
  1478. for(;;) {
  1479. struct inode *inode = d->d_inode;
  1480. if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
  1481. struct audit_chunk *chunk;
  1482. chunk = audit_tree_lookup(inode);
  1483. if (chunk) {
  1484. if (unlikely(!put_tree_ref(context, chunk))) {
  1485. drop = chunk;
  1486. break;
  1487. }
  1488. }
  1489. }
  1490. parent = d->d_parent;
  1491. if (parent == d)
  1492. break;
  1493. d = parent;
  1494. }
  1495. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1496. rcu_read_unlock();
  1497. if (!drop) {
  1498. /* just a race with rename */
  1499. unroll_tree_refs(context, p, count);
  1500. goto retry;
  1501. }
  1502. audit_put_chunk(drop);
  1503. if (grow_tree_refs(context)) {
  1504. /* OK, got more space */
  1505. unroll_tree_refs(context, p, count);
  1506. goto retry;
  1507. }
  1508. /* too bad */
  1509. printk(KERN_WARNING
  1510. "out of memory, audit has lost a tree reference\n");
  1511. unroll_tree_refs(context, p, count);
  1512. audit_set_auditable(context);
  1513. return;
  1514. }
  1515. rcu_read_unlock();
  1516. #endif
  1517. }
  1518. /**
  1519. * audit_getname - add a name to the list
  1520. * @name: name to add
  1521. *
  1522. * Add a name to the list of audit names for this context.
  1523. * Called from fs/namei.c:getname().
  1524. */
  1525. void __audit_getname(const char *name)
  1526. {
  1527. struct audit_context *context = current->audit_context;
  1528. if (IS_ERR(name) || !name)
  1529. return;
  1530. if (!context->in_syscall) {
  1531. #if AUDIT_DEBUG == 2
  1532. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1533. __FILE__, __LINE__, context->serial, name);
  1534. dump_stack();
  1535. #endif
  1536. return;
  1537. }
  1538. BUG_ON(context->name_count >= AUDIT_NAMES);
  1539. context->names[context->name_count].name = name;
  1540. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  1541. context->names[context->name_count].name_put = 1;
  1542. context->names[context->name_count].ino = (unsigned long)-1;
  1543. context->names[context->name_count].osid = 0;
  1544. ++context->name_count;
  1545. if (!context->pwd.dentry) {
  1546. read_lock(&current->fs->lock);
  1547. context->pwd = current->fs->pwd;
  1548. path_get(&current->fs->pwd);
  1549. read_unlock(&current->fs->lock);
  1550. }
  1551. }
  1552. /* audit_putname - intercept a putname request
  1553. * @name: name to intercept and delay for putname
  1554. *
  1555. * If we have stored the name from getname in the audit context,
  1556. * then we delay the putname until syscall exit.
  1557. * Called from include/linux/fs.h:putname().
  1558. */
  1559. void audit_putname(const char *name)
  1560. {
  1561. struct audit_context *context = current->audit_context;
  1562. BUG_ON(!context);
  1563. if (!context->in_syscall) {
  1564. #if AUDIT_DEBUG == 2
  1565. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1566. __FILE__, __LINE__, context->serial, name);
  1567. if (context->name_count) {
  1568. int i;
  1569. for (i = 0; i < context->name_count; i++)
  1570. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1571. context->names[i].name,
  1572. context->names[i].name ?: "(null)");
  1573. }
  1574. #endif
  1575. __putname(name);
  1576. }
  1577. #if AUDIT_DEBUG
  1578. else {
  1579. ++context->put_count;
  1580. if (context->put_count > context->name_count) {
  1581. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1582. " in_syscall=%d putname(%p) name_count=%d"
  1583. " put_count=%d\n",
  1584. __FILE__, __LINE__,
  1585. context->serial, context->major,
  1586. context->in_syscall, name, context->name_count,
  1587. context->put_count);
  1588. dump_stack();
  1589. }
  1590. }
  1591. #endif
  1592. }
  1593. static int audit_inc_name_count(struct audit_context *context,
  1594. const struct inode *inode)
  1595. {
  1596. if (context->name_count >= AUDIT_NAMES) {
  1597. if (inode)
  1598. printk(KERN_DEBUG "name_count maxed, losing inode data: "
  1599. "dev=%02x:%02x, inode=%lu\n",
  1600. MAJOR(inode->i_sb->s_dev),
  1601. MINOR(inode->i_sb->s_dev),
  1602. inode->i_ino);
  1603. else
  1604. printk(KERN_DEBUG "name_count maxed, losing inode data\n");
  1605. return 1;
  1606. }
  1607. context->name_count++;
  1608. #if AUDIT_DEBUG
  1609. context->ino_count++;
  1610. #endif
  1611. return 0;
  1612. }
  1613. /* Copy inode data into an audit_names. */
  1614. static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
  1615. {
  1616. name->ino = inode->i_ino;
  1617. name->dev = inode->i_sb->s_dev;
  1618. name->mode = inode->i_mode;
  1619. name->uid = inode->i_uid;
  1620. name->gid = inode->i_gid;
  1621. name->rdev = inode->i_rdev;
  1622. security_inode_getsecid(inode, &name->osid);
  1623. }
  1624. /**
  1625. * audit_inode - store the inode and device from a lookup
  1626. * @name: name being audited
  1627. * @dentry: dentry being audited
  1628. *
  1629. * Called from fs/namei.c:path_lookup().
  1630. */
  1631. void __audit_inode(const char *name, const struct dentry *dentry)
  1632. {
  1633. int idx;
  1634. struct audit_context *context = current->audit_context;
  1635. const struct inode *inode = dentry->d_inode;
  1636. if (!context->in_syscall)
  1637. return;
  1638. if (context->name_count
  1639. && context->names[context->name_count-1].name
  1640. && context->names[context->name_count-1].name == name)
  1641. idx = context->name_count - 1;
  1642. else if (context->name_count > 1
  1643. && context->names[context->name_count-2].name
  1644. && context->names[context->name_count-2].name == name)
  1645. idx = context->name_count - 2;
  1646. else {
  1647. /* FIXME: how much do we care about inodes that have no
  1648. * associated name? */
  1649. if (audit_inc_name_count(context, inode))
  1650. return;
  1651. idx = context->name_count - 1;
  1652. context->names[idx].name = NULL;
  1653. }
  1654. handle_path(dentry);
  1655. audit_copy_inode(&context->names[idx], inode);
  1656. }
  1657. /**
  1658. * audit_inode_child - collect inode info for created/removed objects
  1659. * @dname: inode's dentry name
  1660. * @dentry: dentry being audited
  1661. * @parent: inode of dentry parent
  1662. *
  1663. * For syscalls that create or remove filesystem objects, audit_inode
  1664. * can only collect information for the filesystem object's parent.
  1665. * This call updates the audit context with the child's information.
  1666. * Syscalls that create a new filesystem object must be hooked after
  1667. * the object is created. Syscalls that remove a filesystem object
  1668. * must be hooked prior, in order to capture the target inode during
  1669. * unsuccessful attempts.
  1670. */
  1671. void __audit_inode_child(const char *dname, const struct dentry *dentry,
  1672. const struct inode *parent)
  1673. {
  1674. int idx;
  1675. struct audit_context *context = current->audit_context;
  1676. const char *found_parent = NULL, *found_child = NULL;
  1677. const struct inode *inode = dentry->d_inode;
  1678. int dirlen = 0;
  1679. if (!context->in_syscall)
  1680. return;
  1681. if (inode)
  1682. handle_one(inode);
  1683. /* determine matching parent */
  1684. if (!dname)
  1685. goto add_names;
  1686. /* parent is more likely, look for it first */
  1687. for (idx = 0; idx < context->name_count; idx++) {
  1688. struct audit_names *n = &context->names[idx];
  1689. if (!n->name)
  1690. continue;
  1691. if (n->ino == parent->i_ino &&
  1692. !audit_compare_dname_path(dname, n->name, &dirlen)) {
  1693. n->name_len = dirlen; /* update parent data in place */
  1694. found_parent = n->name;
  1695. goto add_names;
  1696. }
  1697. }
  1698. /* no matching parent, look for matching child */
  1699. for (idx = 0; idx < context->name_count; idx++) {
  1700. struct audit_names *n = &context->names[idx];
  1701. if (!n->name)
  1702. continue;
  1703. /* strcmp() is the more likely scenario */
  1704. if (!strcmp(dname, n->name) ||
  1705. !audit_compare_dname_path(dname, n->name, &dirlen)) {
  1706. if (inode)
  1707. audit_copy_inode(n, inode);
  1708. else
  1709. n->ino = (unsigned long)-1;
  1710. found_child = n->name;
  1711. goto add_names;
  1712. }
  1713. }
  1714. add_names:
  1715. if (!found_parent) {
  1716. if (audit_inc_name_count(context, parent))
  1717. return;
  1718. idx = context->name_count - 1;
  1719. context->names[idx].name = NULL;
  1720. audit_copy_inode(&context->names[idx], parent);
  1721. }
  1722. if (!found_child) {
  1723. if (audit_inc_name_count(context, inode))
  1724. return;
  1725. idx = context->name_count - 1;
  1726. /* Re-use the name belonging to the slot for a matching parent
  1727. * directory. All names for this context are relinquished in
  1728. * audit_free_names() */
  1729. if (found_parent) {
  1730. context->names[idx].name = found_parent;
  1731. context->names[idx].name_len = AUDIT_NAME_FULL;
  1732. /* don't call __putname() */
  1733. context->names[idx].name_put = 0;
  1734. } else {
  1735. context->names[idx].name = NULL;
  1736. }
  1737. if (inode)
  1738. audit_copy_inode(&context->names[idx], inode);
  1739. else
  1740. context->names[idx].ino = (unsigned long)-1;
  1741. }
  1742. }
  1743. EXPORT_SYMBOL_GPL(__audit_inode_child);
  1744. /**
  1745. * auditsc_get_stamp - get local copies of audit_context values
  1746. * @ctx: audit_context for the task
  1747. * @t: timespec to store time recorded in the audit_context
  1748. * @serial: serial value that is recorded in the audit_context
  1749. *
  1750. * Also sets the context as auditable.
  1751. */
  1752. void auditsc_get_stamp(struct audit_context *ctx,
  1753. struct timespec *t, unsigned int *serial)
  1754. {
  1755. if (!ctx->serial)
  1756. ctx->serial = audit_serial();
  1757. t->tv_sec = ctx->ctime.tv_sec;
  1758. t->tv_nsec = ctx->ctime.tv_nsec;
  1759. *serial = ctx->serial;
  1760. ctx->auditable = 1;
  1761. }
  1762. /* global counter which is incremented every time something logs in */
  1763. static atomic_t session_id = ATOMIC_INIT(0);
  1764. /**
  1765. * audit_set_loginuid - set a task's audit_context loginuid
  1766. * @task: task whose audit context is being modified
  1767. * @loginuid: loginuid value
  1768. *
  1769. * Returns 0.
  1770. *
  1771. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1772. */
  1773. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1774. {
  1775. unsigned int sessionid = atomic_inc_return(&session_id);
  1776. struct audit_context *context = task->audit_context;
  1777. if (context && context->in_syscall) {
  1778. struct audit_buffer *ab;
  1779. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1780. if (ab) {
  1781. audit_log_format(ab, "login pid=%d uid=%u "
  1782. "old auid=%u new auid=%u"
  1783. " old ses=%u new ses=%u",
  1784. task->pid, task->uid,
  1785. task->loginuid, loginuid,
  1786. task->sessionid, sessionid);
  1787. audit_log_end(ab);
  1788. }
  1789. }
  1790. task->sessionid = sessionid;
  1791. task->loginuid = loginuid;
  1792. return 0;
  1793. }
  1794. /**
  1795. * __audit_mq_open - record audit data for a POSIX MQ open
  1796. * @oflag: open flag
  1797. * @mode: mode bits
  1798. * @u_attr: queue attributes
  1799. *
  1800. * Returns 0 for success or NULL context or < 0 on error.
  1801. */
  1802. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1803. {
  1804. struct audit_aux_data_mq_open *ax;
  1805. struct audit_context *context = current->audit_context;
  1806. if (!audit_enabled)
  1807. return 0;
  1808. if (likely(!context))
  1809. return 0;
  1810. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1811. if (!ax)
  1812. return -ENOMEM;
  1813. if (u_attr != NULL) {
  1814. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1815. kfree(ax);
  1816. return -EFAULT;
  1817. }
  1818. } else
  1819. memset(&ax->attr, 0, sizeof(ax->attr));
  1820. ax->oflag = oflag;
  1821. ax->mode = mode;
  1822. ax->d.type = AUDIT_MQ_OPEN;
  1823. ax->d.next = context->aux;
  1824. context->aux = (void *)ax;
  1825. return 0;
  1826. }
  1827. /**
  1828. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1829. * @mqdes: MQ descriptor
  1830. * @msg_len: Message length
  1831. * @msg_prio: Message priority
  1832. * @u_abs_timeout: Message timeout in absolute time
  1833. *
  1834. * Returns 0 for success or NULL context or < 0 on error.
  1835. */
  1836. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1837. const struct timespec __user *u_abs_timeout)
  1838. {
  1839. struct audit_aux_data_mq_sendrecv *ax;
  1840. struct audit_context *context = current->audit_context;
  1841. if (!audit_enabled)
  1842. return 0;
  1843. if (likely(!context))
  1844. return 0;
  1845. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1846. if (!ax)
  1847. return -ENOMEM;
  1848. if (u_abs_timeout != NULL) {
  1849. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1850. kfree(ax);
  1851. return -EFAULT;
  1852. }
  1853. } else
  1854. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1855. ax->mqdes = mqdes;
  1856. ax->msg_len = msg_len;
  1857. ax->msg_prio = msg_prio;
  1858. ax->d.type = AUDIT_MQ_SENDRECV;
  1859. ax->d.next = context->aux;
  1860. context->aux = (void *)ax;
  1861. return 0;
  1862. }
  1863. /**
  1864. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1865. * @mqdes: MQ descriptor
  1866. * @msg_len: Message length
  1867. * @u_msg_prio: Message priority
  1868. * @u_abs_timeout: Message timeout in absolute time
  1869. *
  1870. * Returns 0 for success or NULL context or < 0 on error.
  1871. */
  1872. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1873. unsigned int __user *u_msg_prio,
  1874. const struct timespec __user *u_abs_timeout)
  1875. {
  1876. struct audit_aux_data_mq_sendrecv *ax;
  1877. struct audit_context *context = current->audit_context;
  1878. if (!audit_enabled)
  1879. return 0;
  1880. if (likely(!context))
  1881. return 0;
  1882. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1883. if (!ax)
  1884. return -ENOMEM;
  1885. if (u_msg_prio != NULL) {
  1886. if (get_user(ax->msg_prio, u_msg_prio)) {
  1887. kfree(ax);
  1888. return -EFAULT;
  1889. }
  1890. } else
  1891. ax->msg_prio = 0;
  1892. if (u_abs_timeout != NULL) {
  1893. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1894. kfree(ax);
  1895. return -EFAULT;
  1896. }
  1897. } else
  1898. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1899. ax->mqdes = mqdes;
  1900. ax->msg_len = msg_len;
  1901. ax->d.type = AUDIT_MQ_SENDRECV;
  1902. ax->d.next = context->aux;
  1903. context->aux = (void *)ax;
  1904. return 0;
  1905. }
  1906. /**
  1907. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1908. * @mqdes: MQ descriptor
  1909. * @u_notification: Notification event
  1910. *
  1911. * Returns 0 for success or NULL context or < 0 on error.
  1912. */
  1913. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1914. {
  1915. struct audit_aux_data_mq_notify *ax;
  1916. struct audit_context *context = current->audit_context;
  1917. if (!audit_enabled)
  1918. return 0;
  1919. if (likely(!context))
  1920. return 0;
  1921. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1922. if (!ax)
  1923. return -ENOMEM;
  1924. if (u_notification != NULL) {
  1925. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1926. kfree(ax);
  1927. return -EFAULT;
  1928. }
  1929. } else
  1930. memset(&ax->notification, 0, sizeof(ax->notification));
  1931. ax->mqdes = mqdes;
  1932. ax->d.type = AUDIT_MQ_NOTIFY;
  1933. ax->d.next = context->aux;
  1934. context->aux = (void *)ax;
  1935. return 0;
  1936. }
  1937. /**
  1938. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1939. * @mqdes: MQ descriptor
  1940. * @mqstat: MQ flags
  1941. *
  1942. * Returns 0 for success or NULL context or < 0 on error.
  1943. */
  1944. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1945. {
  1946. struct audit_aux_data_mq_getsetattr *ax;
  1947. struct audit_context *context = current->audit_context;
  1948. if (!audit_enabled)
  1949. return 0;
  1950. if (likely(!context))
  1951. return 0;
  1952. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1953. if (!ax)
  1954. return -ENOMEM;
  1955. ax->mqdes = mqdes;
  1956. ax->mqstat = *mqstat;
  1957. ax->d.type = AUDIT_MQ_GETSETATTR;
  1958. ax->d.next = context->aux;
  1959. context->aux = (void *)ax;
  1960. return 0;
  1961. }
  1962. /**
  1963. * audit_ipc_obj - record audit data for ipc object
  1964. * @ipcp: ipc permissions
  1965. *
  1966. * Returns 0 for success or NULL context or < 0 on error.
  1967. */
  1968. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1969. {
  1970. struct audit_aux_data_ipcctl *ax;
  1971. struct audit_context *context = current->audit_context;
  1972. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1973. if (!ax)
  1974. return -ENOMEM;
  1975. ax->uid = ipcp->uid;
  1976. ax->gid = ipcp->gid;
  1977. ax->mode = ipcp->mode;
  1978. security_ipc_getsecid(ipcp, &ax->osid);
  1979. ax->d.type = AUDIT_IPC;
  1980. ax->d.next = context->aux;
  1981. context->aux = (void *)ax;
  1982. return 0;
  1983. }
  1984. /**
  1985. * audit_ipc_set_perm - record audit data for new ipc permissions
  1986. * @qbytes: msgq bytes
  1987. * @uid: msgq user id
  1988. * @gid: msgq group id
  1989. * @mode: msgq mode (permissions)
  1990. *
  1991. * Returns 0 for success or NULL context or < 0 on error.
  1992. */
  1993. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1994. {
  1995. struct audit_aux_data_ipcctl *ax;
  1996. struct audit_context *context = current->audit_context;
  1997. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1998. if (!ax)
  1999. return -ENOMEM;
  2000. ax->qbytes = qbytes;
  2001. ax->uid = uid;
  2002. ax->gid = gid;
  2003. ax->mode = mode;
  2004. ax->d.type = AUDIT_IPC_SET_PERM;
  2005. ax->d.next = context->aux;
  2006. context->aux = (void *)ax;
  2007. return 0;
  2008. }
  2009. int audit_bprm(struct linux_binprm *bprm)
  2010. {
  2011. struct audit_aux_data_execve *ax;
  2012. struct audit_context *context = current->audit_context;
  2013. if (likely(!audit_enabled || !context || context->dummy))
  2014. return 0;
  2015. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2016. if (!ax)
  2017. return -ENOMEM;
  2018. ax->argc = bprm->argc;
  2019. ax->envc = bprm->envc;
  2020. ax->mm = bprm->mm;
  2021. ax->d.type = AUDIT_EXECVE;
  2022. ax->d.next = context->aux;
  2023. context->aux = (void *)ax;
  2024. return 0;
  2025. }
  2026. /**
  2027. * audit_socketcall - record audit data for sys_socketcall
  2028. * @nargs: number of args
  2029. * @args: args array
  2030. *
  2031. * Returns 0 for success or NULL context or < 0 on error.
  2032. */
  2033. int audit_socketcall(int nargs, unsigned long *args)
  2034. {
  2035. struct audit_aux_data_socketcall *ax;
  2036. struct audit_context *context = current->audit_context;
  2037. if (likely(!context || context->dummy))
  2038. return 0;
  2039. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  2040. if (!ax)
  2041. return -ENOMEM;
  2042. ax->nargs = nargs;
  2043. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  2044. ax->d.type = AUDIT_SOCKETCALL;
  2045. ax->d.next = context->aux;
  2046. context->aux = (void *)ax;
  2047. return 0;
  2048. }
  2049. /**
  2050. * __audit_fd_pair - record audit data for pipe and socketpair
  2051. * @fd1: the first file descriptor
  2052. * @fd2: the second file descriptor
  2053. *
  2054. * Returns 0 for success or NULL context or < 0 on error.
  2055. */
  2056. int __audit_fd_pair(int fd1, int fd2)
  2057. {
  2058. struct audit_context *context = current->audit_context;
  2059. struct audit_aux_data_fd_pair *ax;
  2060. if (likely(!context)) {
  2061. return 0;
  2062. }
  2063. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2064. if (!ax) {
  2065. return -ENOMEM;
  2066. }
  2067. ax->fd[0] = fd1;
  2068. ax->fd[1] = fd2;
  2069. ax->d.type = AUDIT_FD_PAIR;
  2070. ax->d.next = context->aux;
  2071. context->aux = (void *)ax;
  2072. return 0;
  2073. }
  2074. /**
  2075. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  2076. * @len: data length in user space
  2077. * @a: data address in kernel space
  2078. *
  2079. * Returns 0 for success or NULL context or < 0 on error.
  2080. */
  2081. int audit_sockaddr(int len, void *a)
  2082. {
  2083. struct audit_aux_data_sockaddr *ax;
  2084. struct audit_context *context = current->audit_context;
  2085. if (likely(!context || context->dummy))
  2086. return 0;
  2087. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  2088. if (!ax)
  2089. return -ENOMEM;
  2090. ax->len = len;
  2091. memcpy(ax->a, a, len);
  2092. ax->d.type = AUDIT_SOCKADDR;
  2093. ax->d.next = context->aux;
  2094. context->aux = (void *)ax;
  2095. return 0;
  2096. }
  2097. void __audit_ptrace(struct task_struct *t)
  2098. {
  2099. struct audit_context *context = current->audit_context;
  2100. context->target_pid = t->pid;
  2101. context->target_auid = audit_get_loginuid(t);
  2102. context->target_uid = t->uid;
  2103. context->target_sessionid = audit_get_sessionid(t);
  2104. security_task_getsecid(t, &context->target_sid);
  2105. memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
  2106. }
  2107. /**
  2108. * audit_signal_info - record signal info for shutting down audit subsystem
  2109. * @sig: signal value
  2110. * @t: task being signaled
  2111. *
  2112. * If the audit subsystem is being terminated, record the task (pid)
  2113. * and uid that is doing that.
  2114. */
  2115. int __audit_signal_info(int sig, struct task_struct *t)
  2116. {
  2117. struct audit_aux_data_pids *axp;
  2118. struct task_struct *tsk = current;
  2119. struct audit_context *ctx = tsk->audit_context;
  2120. if (audit_pid && t->tgid == audit_pid) {
  2121. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
  2122. audit_sig_pid = tsk->pid;
  2123. if (tsk->loginuid != -1)
  2124. audit_sig_uid = tsk->loginuid;
  2125. else
  2126. audit_sig_uid = tsk->uid;
  2127. security_task_getsecid(tsk, &audit_sig_sid);
  2128. }
  2129. if (!audit_signals || audit_dummy_context())
  2130. return 0;
  2131. }
  2132. /* optimize the common case by putting first signal recipient directly
  2133. * in audit_context */
  2134. if (!ctx->target_pid) {
  2135. ctx->target_pid = t->tgid;
  2136. ctx->target_auid = audit_get_loginuid(t);
  2137. ctx->target_uid = t->uid;
  2138. ctx->target_sessionid = audit_get_sessionid(t);
  2139. security_task_getsecid(t, &ctx->target_sid);
  2140. memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
  2141. return 0;
  2142. }
  2143. axp = (void *)ctx->aux_pids;
  2144. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  2145. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  2146. if (!axp)
  2147. return -ENOMEM;
  2148. axp->d.type = AUDIT_OBJ_PID;
  2149. axp->d.next = ctx->aux_pids;
  2150. ctx->aux_pids = (void *)axp;
  2151. }
  2152. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  2153. axp->target_pid[axp->pid_count] = t->tgid;
  2154. axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
  2155. axp->target_uid[axp->pid_count] = t->uid;
  2156. axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
  2157. security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
  2158. memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
  2159. axp->pid_count++;
  2160. return 0;
  2161. }
  2162. /**
  2163. * audit_core_dumps - record information about processes that end abnormally
  2164. * @signr: signal value
  2165. *
  2166. * If a process ends with a core dump, something fishy is going on and we
  2167. * should record the event for investigation.
  2168. */
  2169. void audit_core_dumps(long signr)
  2170. {
  2171. struct audit_buffer *ab;
  2172. u32 sid;
  2173. uid_t auid = audit_get_loginuid(current);
  2174. unsigned int sessionid = audit_get_sessionid(current);
  2175. if (!audit_enabled)
  2176. return;
  2177. if (signr == SIGQUIT) /* don't care for those */
  2178. return;
  2179. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2180. audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
  2181. auid, current->uid, current->gid, sessionid);
  2182. security_task_getsecid(current, &sid);
  2183. if (sid) {
  2184. char *ctx = NULL;
  2185. u32 len;
  2186. if (security_secid_to_secctx(sid, &ctx, &len))
  2187. audit_log_format(ab, " ssid=%u", sid);
  2188. else {
  2189. audit_log_format(ab, " subj=%s", ctx);
  2190. security_release_secctx(ctx, len);
  2191. }
  2192. }
  2193. audit_log_format(ab, " pid=%d comm=", current->pid);
  2194. audit_log_untrustedstring(ab, current->comm);
  2195. audit_log_format(ab, " sig=%ld", signr);
  2196. audit_log_end(ab);
  2197. }