auditsc.c 72 KB

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