auditsc.c 59 KB

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