auditsc.c 63 KB

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