auditsc.c 72 KB

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