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