auditsc.c 74 KB

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