auditsc.c 62 KB

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