auditsc.c 62 KB

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