auditsc.c 74 KB

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