smackfs.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  1. /*
  2. * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, version 2.
  7. *
  8. * Authors:
  9. * Casey Schaufler <casey@schaufler-ca.com>
  10. * Ahmed S. Darwish <darwish.07@gmail.com>
  11. *
  12. * Special thanks to the authors of selinuxfs.
  13. *
  14. * Karl MacMillan <kmacmillan@tresys.com>
  15. * James Morris <jmorris@redhat.com>
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/security.h>
  21. #include <linux/mutex.h>
  22. #include <linux/slab.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cipso_ipv4.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/ctype.h>
  27. #include <linux/audit.h>
  28. #include <linux/magic.h>
  29. #include "smack.h"
  30. /*
  31. * smackfs pseudo filesystem.
  32. */
  33. enum smk_inos {
  34. SMK_ROOT_INO = 2,
  35. SMK_LOAD = 3, /* load policy */
  36. SMK_CIPSO = 4, /* load label -> CIPSO mapping */
  37. SMK_DOI = 5, /* CIPSO DOI */
  38. SMK_DIRECT = 6, /* CIPSO level indicating direct label */
  39. SMK_AMBIENT = 7, /* internet ambient label */
  40. SMK_NETLBLADDR = 8, /* single label hosts */
  41. SMK_ONLYCAP = 9, /* the only "capable" label */
  42. SMK_LOGGING = 10, /* logging */
  43. SMK_LOAD_SELF = 11, /* task specific rules */
  44. SMK_ACCESSES = 12, /* access policy */
  45. SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
  46. SMK_LOAD2 = 14, /* load policy with long labels */
  47. SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
  48. SMK_ACCESS2 = 16, /* make an access check with long labels */
  49. SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
  50. SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
  51. SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
  52. };
  53. /*
  54. * List locks
  55. */
  56. static DEFINE_MUTEX(smack_cipso_lock);
  57. static DEFINE_MUTEX(smack_ambient_lock);
  58. static DEFINE_MUTEX(smk_netlbladdr_lock);
  59. /*
  60. * This is the "ambient" label for network traffic.
  61. * If it isn't somehow marked, use this.
  62. * It can be reset via smackfs/ambient
  63. */
  64. struct smack_known *smack_net_ambient;
  65. /*
  66. * This is the level in a CIPSO header that indicates a
  67. * smack label is contained directly in the category set.
  68. * It can be reset via smackfs/direct
  69. */
  70. int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
  71. /*
  72. * This is the level in a CIPSO header that indicates a
  73. * secid is contained directly in the category set.
  74. * It can be reset via smackfs/mapped
  75. */
  76. int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
  77. /*
  78. * Unless a process is running with this label even
  79. * having CAP_MAC_OVERRIDE isn't enough to grant
  80. * privilege to violate MAC policy. If no label is
  81. * designated (the NULL case) capabilities apply to
  82. * everyone. It is expected that the hat (^) label
  83. * will be used if any label is used.
  84. */
  85. char *smack_onlycap;
  86. /*
  87. * Certain IP addresses may be designated as single label hosts.
  88. * Packets are sent there unlabeled, but only from tasks that
  89. * can write to the specified label.
  90. */
  91. LIST_HEAD(smk_netlbladdr_list);
  92. /*
  93. * Rule lists are maintained for each label.
  94. * This master list is just for reading /smack/load and /smack/load2.
  95. */
  96. struct smack_master_list {
  97. struct list_head list;
  98. struct smack_rule *smk_rule;
  99. };
  100. LIST_HEAD(smack_rule_list);
  101. struct smack_parsed_rule {
  102. struct smack_known *smk_subject;
  103. char *smk_object;
  104. int smk_access1;
  105. int smk_access2;
  106. };
  107. static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
  108. const char *smack_cipso_option = SMACK_CIPSO_OPTION;
  109. /*
  110. * Values for parsing cipso rules
  111. * SMK_DIGITLEN: Length of a digit field in a rule.
  112. * SMK_CIPSOMIN: Minimum possible cipso rule length.
  113. * SMK_CIPSOMAX: Maximum possible cipso rule length.
  114. */
  115. #define SMK_DIGITLEN 4
  116. #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
  117. #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
  118. /*
  119. * Values for parsing MAC rules
  120. * SMK_ACCESS: Maximum possible combination of access permissions
  121. * SMK_ACCESSLEN: Maximum length for a rule access field
  122. * SMK_LOADLEN: Smack rule length
  123. */
  124. #define SMK_OACCESS "rwxa"
  125. #define SMK_ACCESS "rwxat"
  126. #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
  127. #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
  128. #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
  129. #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
  130. /*
  131. * Stricly for CIPSO level manipulation.
  132. * Set the category bit number in a smack label sized buffer.
  133. */
  134. static inline void smack_catset_bit(unsigned int cat, char *catsetp)
  135. {
  136. if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
  137. return;
  138. catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
  139. }
  140. /**
  141. * smk_netlabel_audit_set - fill a netlbl_audit struct
  142. * @nap: structure to fill
  143. */
  144. static void smk_netlabel_audit_set(struct netlbl_audit *nap)
  145. {
  146. struct smack_known *skp = smk_of_current();
  147. nap->loginuid = audit_get_loginuid(current);
  148. nap->sessionid = audit_get_sessionid(current);
  149. nap->secid = skp->smk_secid;
  150. }
  151. /*
  152. * Value for parsing single label host rules
  153. * "1.2.3.4 X"
  154. */
  155. #define SMK_NETLBLADDRMIN 9
  156. /**
  157. * smk_set_access - add a rule to the rule list or replace an old rule
  158. * @srp: the rule to add or replace
  159. * @rule_list: the list of rules
  160. * @rule_lock: the rule list lock
  161. * @global: if non-zero, indicates a global rule
  162. *
  163. * Looks through the current subject/object/access list for
  164. * the subject/object pair and replaces the access that was
  165. * there. If the pair isn't found add it with the specified
  166. * access.
  167. *
  168. * Returns 0 if nothing goes wrong or -ENOMEM if it fails
  169. * during the allocation of the new pair to add.
  170. */
  171. static int smk_set_access(struct smack_parsed_rule *srp,
  172. struct list_head *rule_list,
  173. struct mutex *rule_lock, int global)
  174. {
  175. struct smack_rule *sp;
  176. struct smack_master_list *smlp;
  177. int found = 0;
  178. int rc = 0;
  179. mutex_lock(rule_lock);
  180. /*
  181. * Because the object label is less likely to match
  182. * than the subject label check it first
  183. */
  184. list_for_each_entry_rcu(sp, rule_list, list) {
  185. if (sp->smk_object == srp->smk_object &&
  186. sp->smk_subject == srp->smk_subject) {
  187. found = 1;
  188. sp->smk_access |= srp->smk_access1;
  189. sp->smk_access &= ~srp->smk_access2;
  190. break;
  191. }
  192. }
  193. if (found == 0) {
  194. sp = kzalloc(sizeof(*sp), GFP_KERNEL);
  195. if (sp == NULL) {
  196. rc = -ENOMEM;
  197. goto out;
  198. }
  199. sp->smk_subject = srp->smk_subject;
  200. sp->smk_object = srp->smk_object;
  201. sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
  202. list_add_rcu(&sp->list, rule_list);
  203. /*
  204. * If this is a global as opposed to self and a new rule
  205. * it needs to get added for reporting.
  206. */
  207. if (global) {
  208. smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
  209. if (smlp != NULL) {
  210. smlp->smk_rule = sp;
  211. list_add_rcu(&smlp->list, &smack_rule_list);
  212. } else
  213. rc = -ENOMEM;
  214. }
  215. }
  216. out:
  217. mutex_unlock(rule_lock);
  218. return rc;
  219. }
  220. /**
  221. * smk_perm_from_str - parse smack accesses from a text string
  222. * @string: a text string that contains a Smack accesses code
  223. *
  224. * Returns an integer with respective bits set for specified accesses.
  225. */
  226. static int smk_perm_from_str(const char *string)
  227. {
  228. int perm = 0;
  229. const char *cp;
  230. for (cp = string; ; cp++)
  231. switch (*cp) {
  232. case '-':
  233. break;
  234. case 'r':
  235. case 'R':
  236. perm |= MAY_READ;
  237. break;
  238. case 'w':
  239. case 'W':
  240. perm |= MAY_WRITE;
  241. break;
  242. case 'x':
  243. case 'X':
  244. perm |= MAY_EXEC;
  245. break;
  246. case 'a':
  247. case 'A':
  248. perm |= MAY_APPEND;
  249. break;
  250. case 't':
  251. case 'T':
  252. perm |= MAY_TRANSMUTE;
  253. break;
  254. default:
  255. return perm;
  256. }
  257. }
  258. /**
  259. * smk_fill_rule - Fill Smack rule from strings
  260. * @subject: subject label string
  261. * @object: object label string
  262. * @access1: access string
  263. * @access2: string with permissions to be removed
  264. * @rule: Smack rule
  265. * @import: if non-zero, import labels
  266. * @len: label length limit
  267. *
  268. * Returns 0 on success, -1 on failure
  269. */
  270. static int smk_fill_rule(const char *subject, const char *object,
  271. const char *access1, const char *access2,
  272. struct smack_parsed_rule *rule, int import,
  273. int len)
  274. {
  275. const char *cp;
  276. struct smack_known *skp;
  277. if (import) {
  278. rule->smk_subject = smk_import_entry(subject, len);
  279. if (rule->smk_subject == NULL)
  280. return -1;
  281. rule->smk_object = smk_import(object, len);
  282. if (rule->smk_object == NULL)
  283. return -1;
  284. } else {
  285. cp = smk_parse_smack(subject, len);
  286. if (cp == NULL)
  287. return -1;
  288. skp = smk_find_entry(cp);
  289. kfree(cp);
  290. if (skp == NULL)
  291. return -1;
  292. rule->smk_subject = skp;
  293. cp = smk_parse_smack(object, len);
  294. if (cp == NULL)
  295. return -1;
  296. skp = smk_find_entry(cp);
  297. kfree(cp);
  298. if (skp == NULL)
  299. return -1;
  300. rule->smk_object = skp->smk_known;
  301. }
  302. rule->smk_access1 = smk_perm_from_str(access1);
  303. if (access2)
  304. rule->smk_access2 = smk_perm_from_str(access2);
  305. else
  306. rule->smk_access2 = ~rule->smk_access1;
  307. return 0;
  308. }
  309. /**
  310. * smk_parse_rule - parse Smack rule from load string
  311. * @data: string to be parsed whose size is SMK_LOADLEN
  312. * @rule: Smack rule
  313. * @import: if non-zero, import labels
  314. *
  315. * Returns 0 on success, -1 on errors.
  316. */
  317. static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
  318. int import)
  319. {
  320. int rc;
  321. rc = smk_fill_rule(data, data + SMK_LABELLEN,
  322. data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
  323. import, SMK_LABELLEN);
  324. return rc;
  325. }
  326. /**
  327. * smk_parse_long_rule - parse Smack rule from rule string
  328. * @data: string to be parsed, null terminated
  329. * @rule: Will be filled with Smack parsed rule
  330. * @import: if non-zero, import labels
  331. * @change: if non-zero, data is from /smack/change-rule
  332. *
  333. * Returns 0 on success, -1 on failure
  334. */
  335. static int smk_parse_long_rule(const char *data, struct smack_parsed_rule *rule,
  336. int import, int change)
  337. {
  338. char *subject;
  339. char *object;
  340. char *access1;
  341. char *access2;
  342. int datalen;
  343. int rc = -1;
  344. /* This is inefficient */
  345. datalen = strlen(data);
  346. /* Our first element can be 64 + \0 with no spaces */
  347. subject = kzalloc(datalen + 1, GFP_KERNEL);
  348. if (subject == NULL)
  349. return -1;
  350. object = kzalloc(datalen, GFP_KERNEL);
  351. if (object == NULL)
  352. goto free_out_s;
  353. access1 = kzalloc(datalen, GFP_KERNEL);
  354. if (access1 == NULL)
  355. goto free_out_o;
  356. access2 = kzalloc(datalen, GFP_KERNEL);
  357. if (access2 == NULL)
  358. goto free_out_a;
  359. if (change) {
  360. if (sscanf(data, "%s %s %s %s",
  361. subject, object, access1, access2) == 4)
  362. rc = smk_fill_rule(subject, object, access1, access2,
  363. rule, import, 0);
  364. } else {
  365. if (sscanf(data, "%s %s %s", subject, object, access1) == 3)
  366. rc = smk_fill_rule(subject, object, access1, NULL,
  367. rule, import, 0);
  368. }
  369. kfree(access2);
  370. free_out_a:
  371. kfree(access1);
  372. free_out_o:
  373. kfree(object);
  374. free_out_s:
  375. kfree(subject);
  376. return rc;
  377. }
  378. #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
  379. #define SMK_LONG_FMT 1 /* Variable long label format */
  380. #define SMK_CHANGE_FMT 2 /* Rule modification format */
  381. /**
  382. * smk_write_rules_list - write() for any /smack rule file
  383. * @file: file pointer, not actually used
  384. * @buf: where to get the data from
  385. * @count: bytes sent
  386. * @ppos: where to start - must be 0
  387. * @rule_list: the list of rules to write to
  388. * @rule_lock: lock for the rule list
  389. * @format: /smack/load or /smack/load2 or /smack/change-rule format.
  390. *
  391. * Get one smack access rule from above.
  392. * The format for SMK_LONG_FMT is:
  393. * "subject<whitespace>object<whitespace>access[<whitespace>...]"
  394. * The format for SMK_FIXED24_FMT is exactly:
  395. * "subject object rwxat"
  396. * The format for SMK_CHANGE_FMT is:
  397. * "subject<whitespace>object<whitespace>
  398. * acc_enable<whitespace>acc_disable[<whitespace>...]"
  399. */
  400. static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
  401. size_t count, loff_t *ppos,
  402. struct list_head *rule_list,
  403. struct mutex *rule_lock, int format)
  404. {
  405. struct smack_parsed_rule *rule;
  406. char *data;
  407. int datalen;
  408. int rc = -EINVAL;
  409. int load = 0;
  410. /*
  411. * No partial writes.
  412. * Enough data must be present.
  413. */
  414. if (*ppos != 0)
  415. return -EINVAL;
  416. if (format == SMK_FIXED24_FMT) {
  417. /*
  418. * Minor hack for backward compatibility
  419. */
  420. if (count != SMK_OLOADLEN && count != SMK_LOADLEN)
  421. return -EINVAL;
  422. datalen = SMK_LOADLEN;
  423. } else
  424. datalen = count + 1;
  425. data = kzalloc(datalen, GFP_KERNEL);
  426. if (data == NULL)
  427. return -ENOMEM;
  428. if (copy_from_user(data, buf, count) != 0) {
  429. rc = -EFAULT;
  430. goto out;
  431. }
  432. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  433. if (rule == NULL) {
  434. rc = -ENOMEM;
  435. goto out;
  436. }
  437. if (format == SMK_LONG_FMT) {
  438. /*
  439. * Be sure the data string is terminated.
  440. */
  441. data[count] = '\0';
  442. if (smk_parse_long_rule(data, rule, 1, 0))
  443. goto out_free_rule;
  444. } else if (format == SMK_CHANGE_FMT) {
  445. data[count] = '\0';
  446. if (smk_parse_long_rule(data, rule, 1, 1))
  447. goto out_free_rule;
  448. } else {
  449. /*
  450. * More on the minor hack for backward compatibility
  451. */
  452. if (count == (SMK_OLOADLEN))
  453. data[SMK_OLOADLEN] = '-';
  454. if (smk_parse_rule(data, rule, 1))
  455. goto out_free_rule;
  456. }
  457. if (rule_list == NULL) {
  458. load = 1;
  459. rule_list = &rule->smk_subject->smk_rules;
  460. rule_lock = &rule->smk_subject->smk_rules_lock;
  461. }
  462. rc = smk_set_access(rule, rule_list, rule_lock, load);
  463. if (rc == 0) {
  464. rc = count;
  465. goto out;
  466. }
  467. out_free_rule:
  468. kfree(rule);
  469. out:
  470. kfree(data);
  471. return rc;
  472. }
  473. /*
  474. * Core logic for smackfs seq list operations.
  475. */
  476. static void *smk_seq_start(struct seq_file *s, loff_t *pos,
  477. struct list_head *head)
  478. {
  479. struct list_head *list;
  480. /*
  481. * This is 0 the first time through.
  482. */
  483. if (s->index == 0)
  484. s->private = head;
  485. if (s->private == NULL)
  486. return NULL;
  487. list = s->private;
  488. if (list_empty(list))
  489. return NULL;
  490. if (s->index == 0)
  491. return list->next;
  492. return list;
  493. }
  494. static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
  495. struct list_head *head)
  496. {
  497. struct list_head *list = v;
  498. if (list_is_last(list, head)) {
  499. s->private = NULL;
  500. return NULL;
  501. }
  502. s->private = list->next;
  503. return list->next;
  504. }
  505. static void smk_seq_stop(struct seq_file *s, void *v)
  506. {
  507. /* No-op */
  508. }
  509. static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
  510. {
  511. /*
  512. * Don't show any rules with label names too long for
  513. * interface file (/smack/load or /smack/load2)
  514. * because you should expect to be able to write
  515. * anything you read back.
  516. */
  517. if (strlen(srp->smk_subject->smk_known) >= max ||
  518. strlen(srp->smk_object) >= max)
  519. return;
  520. if (srp->smk_access == 0)
  521. return;
  522. seq_printf(s, "%s %s", srp->smk_subject->smk_known, srp->smk_object);
  523. seq_putc(s, ' ');
  524. if (srp->smk_access & MAY_READ)
  525. seq_putc(s, 'r');
  526. if (srp->smk_access & MAY_WRITE)
  527. seq_putc(s, 'w');
  528. if (srp->smk_access & MAY_EXEC)
  529. seq_putc(s, 'x');
  530. if (srp->smk_access & MAY_APPEND)
  531. seq_putc(s, 'a');
  532. if (srp->smk_access & MAY_TRANSMUTE)
  533. seq_putc(s, 't');
  534. seq_putc(s, '\n');
  535. }
  536. /*
  537. * Seq_file read operations for /smack/load
  538. */
  539. static void *load2_seq_start(struct seq_file *s, loff_t *pos)
  540. {
  541. return smk_seq_start(s, pos, &smack_rule_list);
  542. }
  543. static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  544. {
  545. return smk_seq_next(s, v, pos, &smack_rule_list);
  546. }
  547. static int load_seq_show(struct seq_file *s, void *v)
  548. {
  549. struct list_head *list = v;
  550. struct smack_master_list *smlp =
  551. list_entry(list, struct smack_master_list, list);
  552. smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
  553. return 0;
  554. }
  555. static const struct seq_operations load_seq_ops = {
  556. .start = load2_seq_start,
  557. .next = load2_seq_next,
  558. .show = load_seq_show,
  559. .stop = smk_seq_stop,
  560. };
  561. /**
  562. * smk_open_load - open() for /smack/load
  563. * @inode: inode structure representing file
  564. * @file: "load" file pointer
  565. *
  566. * For reading, use load_seq_* seq_file reading operations.
  567. */
  568. static int smk_open_load(struct inode *inode, struct file *file)
  569. {
  570. return seq_open(file, &load_seq_ops);
  571. }
  572. /**
  573. * smk_write_load - write() for /smack/load
  574. * @file: file pointer, not actually used
  575. * @buf: where to get the data from
  576. * @count: bytes sent
  577. * @ppos: where to start - must be 0
  578. *
  579. */
  580. static ssize_t smk_write_load(struct file *file, const char __user *buf,
  581. size_t count, loff_t *ppos)
  582. {
  583. /*
  584. * Must have privilege.
  585. * No partial writes.
  586. * Enough data must be present.
  587. */
  588. if (!smack_privileged(CAP_MAC_ADMIN))
  589. return -EPERM;
  590. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  591. SMK_FIXED24_FMT);
  592. }
  593. static const struct file_operations smk_load_ops = {
  594. .open = smk_open_load,
  595. .read = seq_read,
  596. .llseek = seq_lseek,
  597. .write = smk_write_load,
  598. .release = seq_release,
  599. };
  600. /**
  601. * smk_cipso_doi - initialize the CIPSO domain
  602. */
  603. static void smk_cipso_doi(void)
  604. {
  605. int rc;
  606. struct cipso_v4_doi *doip;
  607. struct netlbl_audit nai;
  608. smk_netlabel_audit_set(&nai);
  609. rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
  610. if (rc != 0)
  611. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  612. __func__, __LINE__, rc);
  613. doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
  614. if (doip == NULL)
  615. panic("smack: Failed to initialize cipso DOI.\n");
  616. doip->map.std = NULL;
  617. doip->doi = smk_cipso_doi_value;
  618. doip->type = CIPSO_V4_MAP_PASS;
  619. doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
  620. for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
  621. doip->tags[rc] = CIPSO_V4_TAG_INVALID;
  622. rc = netlbl_cfg_cipsov4_add(doip, &nai);
  623. if (rc != 0) {
  624. printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
  625. __func__, __LINE__, rc);
  626. kfree(doip);
  627. return;
  628. }
  629. rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
  630. if (rc != 0) {
  631. printk(KERN_WARNING "%s:%d map add rc = %d\n",
  632. __func__, __LINE__, rc);
  633. kfree(doip);
  634. return;
  635. }
  636. }
  637. /**
  638. * smk_unlbl_ambient - initialize the unlabeled domain
  639. * @oldambient: previous domain string
  640. */
  641. static void smk_unlbl_ambient(char *oldambient)
  642. {
  643. int rc;
  644. struct netlbl_audit nai;
  645. smk_netlabel_audit_set(&nai);
  646. if (oldambient != NULL) {
  647. rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
  648. if (rc != 0)
  649. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  650. __func__, __LINE__, rc);
  651. }
  652. if (smack_net_ambient == NULL)
  653. smack_net_ambient = &smack_known_floor;
  654. rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
  655. NULL, NULL, &nai);
  656. if (rc != 0)
  657. printk(KERN_WARNING "%s:%d add rc = %d\n",
  658. __func__, __LINE__, rc);
  659. }
  660. /*
  661. * Seq_file read operations for /smack/cipso
  662. */
  663. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  664. {
  665. return smk_seq_start(s, pos, &smack_known_list);
  666. }
  667. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  668. {
  669. return smk_seq_next(s, v, pos, &smack_known_list);
  670. }
  671. /*
  672. * Print cipso labels in format:
  673. * label level[/cat[,cat]]
  674. */
  675. static int cipso_seq_show(struct seq_file *s, void *v)
  676. {
  677. struct list_head *list = v;
  678. struct smack_known *skp =
  679. list_entry(list, struct smack_known, list);
  680. struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  681. char sep = '/';
  682. int i;
  683. /*
  684. * Don't show a label that could not have been set using
  685. * /smack/cipso. This is in support of the notion that
  686. * anything read from /smack/cipso ought to be writeable
  687. * to /smack/cipso.
  688. *
  689. * /smack/cipso2 should be used instead.
  690. */
  691. if (strlen(skp->smk_known) >= SMK_LABELLEN)
  692. return 0;
  693. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  694. for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
  695. i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
  696. seq_printf(s, "%c%d", sep, i);
  697. sep = ',';
  698. }
  699. seq_putc(s, '\n');
  700. return 0;
  701. }
  702. static const struct seq_operations cipso_seq_ops = {
  703. .start = cipso_seq_start,
  704. .next = cipso_seq_next,
  705. .show = cipso_seq_show,
  706. .stop = smk_seq_stop,
  707. };
  708. /**
  709. * smk_open_cipso - open() for /smack/cipso
  710. * @inode: inode structure representing file
  711. * @file: "cipso" file pointer
  712. *
  713. * Connect our cipso_seq_* operations with /smack/cipso
  714. * file_operations
  715. */
  716. static int smk_open_cipso(struct inode *inode, struct file *file)
  717. {
  718. return seq_open(file, &cipso_seq_ops);
  719. }
  720. /**
  721. * smk_set_cipso - do the work for write() for cipso and cipso2
  722. * @file: file pointer, not actually used
  723. * @buf: where to get the data from
  724. * @count: bytes sent
  725. * @ppos: where to start
  726. * @format: /smack/cipso or /smack/cipso2
  727. *
  728. * Accepts only one cipso rule per write call.
  729. * Returns number of bytes written or error code, as appropriate
  730. */
  731. static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
  732. size_t count, loff_t *ppos, int format)
  733. {
  734. struct smack_known *skp;
  735. struct netlbl_lsm_secattr ncats;
  736. char mapcatset[SMK_CIPSOLEN];
  737. int maplevel;
  738. unsigned int cat;
  739. int catlen;
  740. ssize_t rc = -EINVAL;
  741. char *data = NULL;
  742. char *rule;
  743. int ret;
  744. int i;
  745. /*
  746. * Must have privilege.
  747. * No partial writes.
  748. * Enough data must be present.
  749. */
  750. if (!smack_privileged(CAP_MAC_ADMIN))
  751. return -EPERM;
  752. if (*ppos != 0)
  753. return -EINVAL;
  754. if (format == SMK_FIXED24_FMT &&
  755. (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
  756. return -EINVAL;
  757. data = kzalloc(count + 1, GFP_KERNEL);
  758. if (data == NULL)
  759. return -ENOMEM;
  760. if (copy_from_user(data, buf, count) != 0) {
  761. rc = -EFAULT;
  762. goto unlockedout;
  763. }
  764. data[count] = '\0';
  765. rule = data;
  766. /*
  767. * Only allow one writer at a time. Writes should be
  768. * quite rare and small in any case.
  769. */
  770. mutex_lock(&smack_cipso_lock);
  771. skp = smk_import_entry(rule, 0);
  772. if (skp == NULL)
  773. goto out;
  774. if (format == SMK_FIXED24_FMT)
  775. rule += SMK_LABELLEN;
  776. else
  777. rule += strlen(skp->smk_known) + 1;
  778. ret = sscanf(rule, "%d", &maplevel);
  779. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  780. goto out;
  781. rule += SMK_DIGITLEN;
  782. ret = sscanf(rule, "%d", &catlen);
  783. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  784. goto out;
  785. if (format == SMK_FIXED24_FMT &&
  786. count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  787. goto out;
  788. memset(mapcatset, 0, sizeof(mapcatset));
  789. for (i = 0; i < catlen; i++) {
  790. rule += SMK_DIGITLEN;
  791. ret = sscanf(rule, "%u", &cat);
  792. if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
  793. goto out;
  794. smack_catset_bit(cat, mapcatset);
  795. }
  796. rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
  797. if (rc >= 0) {
  798. netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat);
  799. skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
  800. skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
  801. rc = count;
  802. }
  803. out:
  804. mutex_unlock(&smack_cipso_lock);
  805. unlockedout:
  806. kfree(data);
  807. return rc;
  808. }
  809. /**
  810. * smk_write_cipso - write() for /smack/cipso
  811. * @file: file pointer, not actually used
  812. * @buf: where to get the data from
  813. * @count: bytes sent
  814. * @ppos: where to start
  815. *
  816. * Accepts only one cipso rule per write call.
  817. * Returns number of bytes written or error code, as appropriate
  818. */
  819. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  820. size_t count, loff_t *ppos)
  821. {
  822. return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
  823. }
  824. static const struct file_operations smk_cipso_ops = {
  825. .open = smk_open_cipso,
  826. .read = seq_read,
  827. .llseek = seq_lseek,
  828. .write = smk_write_cipso,
  829. .release = seq_release,
  830. };
  831. /*
  832. * Seq_file read operations for /smack/cipso2
  833. */
  834. /*
  835. * Print cipso labels in format:
  836. * label level[/cat[,cat]]
  837. */
  838. static int cipso2_seq_show(struct seq_file *s, void *v)
  839. {
  840. struct list_head *list = v;
  841. struct smack_known *skp =
  842. list_entry(list, struct smack_known, list);
  843. struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  844. char sep = '/';
  845. int i;
  846. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  847. for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
  848. i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
  849. seq_printf(s, "%c%d", sep, i);
  850. sep = ',';
  851. }
  852. seq_putc(s, '\n');
  853. return 0;
  854. }
  855. static const struct seq_operations cipso2_seq_ops = {
  856. .start = cipso_seq_start,
  857. .next = cipso_seq_next,
  858. .show = cipso2_seq_show,
  859. .stop = smk_seq_stop,
  860. };
  861. /**
  862. * smk_open_cipso2 - open() for /smack/cipso2
  863. * @inode: inode structure representing file
  864. * @file: "cipso2" file pointer
  865. *
  866. * Connect our cipso_seq_* operations with /smack/cipso2
  867. * file_operations
  868. */
  869. static int smk_open_cipso2(struct inode *inode, struct file *file)
  870. {
  871. return seq_open(file, &cipso2_seq_ops);
  872. }
  873. /**
  874. * smk_write_cipso2 - write() for /smack/cipso2
  875. * @file: file pointer, not actually used
  876. * @buf: where to get the data from
  877. * @count: bytes sent
  878. * @ppos: where to start
  879. *
  880. * Accepts only one cipso rule per write call.
  881. * Returns number of bytes written or error code, as appropriate
  882. */
  883. static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
  884. size_t count, loff_t *ppos)
  885. {
  886. return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
  887. }
  888. static const struct file_operations smk_cipso2_ops = {
  889. .open = smk_open_cipso2,
  890. .read = seq_read,
  891. .llseek = seq_lseek,
  892. .write = smk_write_cipso2,
  893. .release = seq_release,
  894. };
  895. /*
  896. * Seq_file read operations for /smack/netlabel
  897. */
  898. static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
  899. {
  900. return smk_seq_start(s, pos, &smk_netlbladdr_list);
  901. }
  902. static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  903. {
  904. return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
  905. }
  906. #define BEBITS (sizeof(__be32) * 8)
  907. /*
  908. * Print host/label pairs
  909. */
  910. static int netlbladdr_seq_show(struct seq_file *s, void *v)
  911. {
  912. struct list_head *list = v;
  913. struct smk_netlbladdr *skp =
  914. list_entry(list, struct smk_netlbladdr, list);
  915. unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
  916. int maskn;
  917. u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
  918. for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
  919. seq_printf(s, "%u.%u.%u.%u/%d %s\n",
  920. hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
  921. return 0;
  922. }
  923. static const struct seq_operations netlbladdr_seq_ops = {
  924. .start = netlbladdr_seq_start,
  925. .next = netlbladdr_seq_next,
  926. .show = netlbladdr_seq_show,
  927. .stop = smk_seq_stop,
  928. };
  929. /**
  930. * smk_open_netlbladdr - open() for /smack/netlabel
  931. * @inode: inode structure representing file
  932. * @file: "netlabel" file pointer
  933. *
  934. * Connect our netlbladdr_seq_* operations with /smack/netlabel
  935. * file_operations
  936. */
  937. static int smk_open_netlbladdr(struct inode *inode, struct file *file)
  938. {
  939. return seq_open(file, &netlbladdr_seq_ops);
  940. }
  941. /**
  942. * smk_netlbladdr_insert
  943. * @new : netlabel to insert
  944. *
  945. * This helper insert netlabel in the smack_netlbladdrs list
  946. * sorted by netmask length (longest to smallest)
  947. * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
  948. *
  949. */
  950. static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
  951. {
  952. struct smk_netlbladdr *m, *m_next;
  953. if (list_empty(&smk_netlbladdr_list)) {
  954. list_add_rcu(&new->list, &smk_netlbladdr_list);
  955. return;
  956. }
  957. m = list_entry_rcu(smk_netlbladdr_list.next,
  958. struct smk_netlbladdr, list);
  959. /* the comparison '>' is a bit hacky, but works */
  960. if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
  961. list_add_rcu(&new->list, &smk_netlbladdr_list);
  962. return;
  963. }
  964. list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
  965. if (list_is_last(&m->list, &smk_netlbladdr_list)) {
  966. list_add_rcu(&new->list, &m->list);
  967. return;
  968. }
  969. m_next = list_entry_rcu(m->list.next,
  970. struct smk_netlbladdr, list);
  971. if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
  972. list_add_rcu(&new->list, &m->list);
  973. return;
  974. }
  975. }
  976. }
  977. /**
  978. * smk_write_netlbladdr - write() for /smack/netlabel
  979. * @file: file pointer, not actually used
  980. * @buf: where to get the data from
  981. * @count: bytes sent
  982. * @ppos: where to start
  983. *
  984. * Accepts only one netlbladdr per write call.
  985. * Returns number of bytes written or error code, as appropriate
  986. */
  987. static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
  988. size_t count, loff_t *ppos)
  989. {
  990. struct smk_netlbladdr *skp;
  991. struct sockaddr_in newname;
  992. char *smack;
  993. char *sp;
  994. char *data;
  995. char *host = (char *)&newname.sin_addr.s_addr;
  996. int rc;
  997. struct netlbl_audit audit_info;
  998. struct in_addr mask;
  999. unsigned int m;
  1000. int found;
  1001. u32 mask_bits = (1<<31);
  1002. __be32 nsa;
  1003. u32 temp_mask;
  1004. /*
  1005. * Must have privilege.
  1006. * No partial writes.
  1007. * Enough data must be present.
  1008. * "<addr/mask, as a.b.c.d/e><space><label>"
  1009. * "<addr, as a.b.c.d><space><label>"
  1010. */
  1011. if (!smack_privileged(CAP_MAC_ADMIN))
  1012. return -EPERM;
  1013. if (*ppos != 0)
  1014. return -EINVAL;
  1015. if (count < SMK_NETLBLADDRMIN)
  1016. return -EINVAL;
  1017. data = kzalloc(count + 1, GFP_KERNEL);
  1018. if (data == NULL)
  1019. return -ENOMEM;
  1020. if (copy_from_user(data, buf, count) != 0) {
  1021. rc = -EFAULT;
  1022. goto free_data_out;
  1023. }
  1024. smack = kzalloc(count + 1, GFP_KERNEL);
  1025. if (smack == NULL) {
  1026. rc = -ENOMEM;
  1027. goto free_data_out;
  1028. }
  1029. data[count] = '\0';
  1030. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
  1031. &host[0], &host[1], &host[2], &host[3], &m, smack);
  1032. if (rc != 6) {
  1033. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  1034. &host[0], &host[1], &host[2], &host[3], smack);
  1035. if (rc != 5) {
  1036. rc = -EINVAL;
  1037. goto free_out;
  1038. }
  1039. m = BEBITS;
  1040. }
  1041. if (m > BEBITS) {
  1042. rc = -EINVAL;
  1043. goto free_out;
  1044. }
  1045. /*
  1046. * If smack begins with '-', it is an option, don't import it
  1047. */
  1048. if (smack[0] != '-') {
  1049. sp = smk_import(smack, 0);
  1050. if (sp == NULL) {
  1051. rc = -EINVAL;
  1052. goto free_out;
  1053. }
  1054. } else {
  1055. /* check known options */
  1056. if (strcmp(smack, smack_cipso_option) == 0)
  1057. sp = (char *)smack_cipso_option;
  1058. else {
  1059. rc = -EINVAL;
  1060. goto free_out;
  1061. }
  1062. }
  1063. for (temp_mask = 0; m > 0; m--) {
  1064. temp_mask |= mask_bits;
  1065. mask_bits >>= 1;
  1066. }
  1067. mask.s_addr = cpu_to_be32(temp_mask);
  1068. newname.sin_addr.s_addr &= mask.s_addr;
  1069. /*
  1070. * Only allow one writer at a time. Writes should be
  1071. * quite rare and small in any case.
  1072. */
  1073. mutex_lock(&smk_netlbladdr_lock);
  1074. nsa = newname.sin_addr.s_addr;
  1075. /* try to find if the prefix is already in the list */
  1076. found = 0;
  1077. list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
  1078. if (skp->smk_host.sin_addr.s_addr == nsa &&
  1079. skp->smk_mask.s_addr == mask.s_addr) {
  1080. found = 1;
  1081. break;
  1082. }
  1083. }
  1084. smk_netlabel_audit_set(&audit_info);
  1085. if (found == 0) {
  1086. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  1087. if (skp == NULL)
  1088. rc = -ENOMEM;
  1089. else {
  1090. rc = 0;
  1091. skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
  1092. skp->smk_mask.s_addr = mask.s_addr;
  1093. skp->smk_label = sp;
  1094. smk_netlbladdr_insert(skp);
  1095. }
  1096. } else {
  1097. /* we delete the unlabeled entry, only if the previous label
  1098. * wasn't the special CIPSO option */
  1099. if (skp->smk_label != smack_cipso_option)
  1100. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  1101. &skp->smk_host.sin_addr, &skp->smk_mask,
  1102. PF_INET, &audit_info);
  1103. else
  1104. rc = 0;
  1105. skp->smk_label = sp;
  1106. }
  1107. /*
  1108. * Now tell netlabel about the single label nature of
  1109. * this host so that incoming packets get labeled.
  1110. * but only if we didn't get the special CIPSO option
  1111. */
  1112. if (rc == 0 && sp != smack_cipso_option)
  1113. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  1114. &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
  1115. smack_to_secid(skp->smk_label), &audit_info);
  1116. if (rc == 0)
  1117. rc = count;
  1118. mutex_unlock(&smk_netlbladdr_lock);
  1119. free_out:
  1120. kfree(smack);
  1121. free_data_out:
  1122. kfree(data);
  1123. return rc;
  1124. }
  1125. static const struct file_operations smk_netlbladdr_ops = {
  1126. .open = smk_open_netlbladdr,
  1127. .read = seq_read,
  1128. .llseek = seq_lseek,
  1129. .write = smk_write_netlbladdr,
  1130. .release = seq_release,
  1131. };
  1132. /**
  1133. * smk_read_doi - read() for /smack/doi
  1134. * @filp: file pointer, not actually used
  1135. * @buf: where to put the result
  1136. * @count: maximum to send along
  1137. * @ppos: where to start
  1138. *
  1139. * Returns number of bytes read or error code, as appropriate
  1140. */
  1141. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  1142. size_t count, loff_t *ppos)
  1143. {
  1144. char temp[80];
  1145. ssize_t rc;
  1146. if (*ppos != 0)
  1147. return 0;
  1148. sprintf(temp, "%d", smk_cipso_doi_value);
  1149. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1150. return rc;
  1151. }
  1152. /**
  1153. * smk_write_doi - write() for /smack/doi
  1154. * @file: file pointer, not actually used
  1155. * @buf: where to get the data from
  1156. * @count: bytes sent
  1157. * @ppos: where to start
  1158. *
  1159. * Returns number of bytes written or error code, as appropriate
  1160. */
  1161. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  1162. size_t count, loff_t *ppos)
  1163. {
  1164. char temp[80];
  1165. int i;
  1166. if (!smack_privileged(CAP_MAC_ADMIN))
  1167. return -EPERM;
  1168. if (count >= sizeof(temp) || count == 0)
  1169. return -EINVAL;
  1170. if (copy_from_user(temp, buf, count) != 0)
  1171. return -EFAULT;
  1172. temp[count] = '\0';
  1173. if (sscanf(temp, "%d", &i) != 1)
  1174. return -EINVAL;
  1175. smk_cipso_doi_value = i;
  1176. smk_cipso_doi();
  1177. return count;
  1178. }
  1179. static const struct file_operations smk_doi_ops = {
  1180. .read = smk_read_doi,
  1181. .write = smk_write_doi,
  1182. .llseek = default_llseek,
  1183. };
  1184. /**
  1185. * smk_read_direct - read() for /smack/direct
  1186. * @filp: file pointer, not actually used
  1187. * @buf: where to put the result
  1188. * @count: maximum to send along
  1189. * @ppos: where to start
  1190. *
  1191. * Returns number of bytes read or error code, as appropriate
  1192. */
  1193. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  1194. size_t count, loff_t *ppos)
  1195. {
  1196. char temp[80];
  1197. ssize_t rc;
  1198. if (*ppos != 0)
  1199. return 0;
  1200. sprintf(temp, "%d", smack_cipso_direct);
  1201. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1202. return rc;
  1203. }
  1204. /**
  1205. * smk_write_direct - write() for /smack/direct
  1206. * @file: file pointer, not actually used
  1207. * @buf: where to get the data from
  1208. * @count: bytes sent
  1209. * @ppos: where to start
  1210. *
  1211. * Returns number of bytes written or error code, as appropriate
  1212. */
  1213. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  1214. size_t count, loff_t *ppos)
  1215. {
  1216. struct smack_known *skp;
  1217. char temp[80];
  1218. int i;
  1219. if (!smack_privileged(CAP_MAC_ADMIN))
  1220. return -EPERM;
  1221. if (count >= sizeof(temp) || count == 0)
  1222. return -EINVAL;
  1223. if (copy_from_user(temp, buf, count) != 0)
  1224. return -EFAULT;
  1225. temp[count] = '\0';
  1226. if (sscanf(temp, "%d", &i) != 1)
  1227. return -EINVAL;
  1228. /*
  1229. * Don't do anything if the value hasn't actually changed.
  1230. * If it is changing reset the level on entries that were
  1231. * set up to be direct when they were created.
  1232. */
  1233. if (smack_cipso_direct != i) {
  1234. mutex_lock(&smack_known_lock);
  1235. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1236. if (skp->smk_netlabel.attr.mls.lvl ==
  1237. smack_cipso_direct)
  1238. skp->smk_netlabel.attr.mls.lvl = i;
  1239. smack_cipso_direct = i;
  1240. mutex_unlock(&smack_known_lock);
  1241. }
  1242. return count;
  1243. }
  1244. static const struct file_operations smk_direct_ops = {
  1245. .read = smk_read_direct,
  1246. .write = smk_write_direct,
  1247. .llseek = default_llseek,
  1248. };
  1249. /**
  1250. * smk_read_mapped - read() for /smack/mapped
  1251. * @filp: file pointer, not actually used
  1252. * @buf: where to put the result
  1253. * @count: maximum to send along
  1254. * @ppos: where to start
  1255. *
  1256. * Returns number of bytes read or error code, as appropriate
  1257. */
  1258. static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
  1259. size_t count, loff_t *ppos)
  1260. {
  1261. char temp[80];
  1262. ssize_t rc;
  1263. if (*ppos != 0)
  1264. return 0;
  1265. sprintf(temp, "%d", smack_cipso_mapped);
  1266. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1267. return rc;
  1268. }
  1269. /**
  1270. * smk_write_mapped - write() for /smack/mapped
  1271. * @file: file pointer, not actually used
  1272. * @buf: where to get the data from
  1273. * @count: bytes sent
  1274. * @ppos: where to start
  1275. *
  1276. * Returns number of bytes written or error code, as appropriate
  1277. */
  1278. static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
  1279. size_t count, loff_t *ppos)
  1280. {
  1281. struct smack_known *skp;
  1282. char temp[80];
  1283. int i;
  1284. if (!smack_privileged(CAP_MAC_ADMIN))
  1285. return -EPERM;
  1286. if (count >= sizeof(temp) || count == 0)
  1287. return -EINVAL;
  1288. if (copy_from_user(temp, buf, count) != 0)
  1289. return -EFAULT;
  1290. temp[count] = '\0';
  1291. if (sscanf(temp, "%d", &i) != 1)
  1292. return -EINVAL;
  1293. /*
  1294. * Don't do anything if the value hasn't actually changed.
  1295. * If it is changing reset the level on entries that were
  1296. * set up to be mapped when they were created.
  1297. */
  1298. if (smack_cipso_mapped != i) {
  1299. mutex_lock(&smack_known_lock);
  1300. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1301. if (skp->smk_netlabel.attr.mls.lvl ==
  1302. smack_cipso_mapped)
  1303. skp->smk_netlabel.attr.mls.lvl = i;
  1304. smack_cipso_mapped = i;
  1305. mutex_unlock(&smack_known_lock);
  1306. }
  1307. return count;
  1308. }
  1309. static const struct file_operations smk_mapped_ops = {
  1310. .read = smk_read_mapped,
  1311. .write = smk_write_mapped,
  1312. .llseek = default_llseek,
  1313. };
  1314. /**
  1315. * smk_read_ambient - read() for /smack/ambient
  1316. * @filp: file pointer, not actually used
  1317. * @buf: where to put the result
  1318. * @cn: maximum to send along
  1319. * @ppos: where to start
  1320. *
  1321. * Returns number of bytes read or error code, as appropriate
  1322. */
  1323. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  1324. size_t cn, loff_t *ppos)
  1325. {
  1326. ssize_t rc;
  1327. int asize;
  1328. if (*ppos != 0)
  1329. return 0;
  1330. /*
  1331. * Being careful to avoid a problem in the case where
  1332. * smack_net_ambient gets changed in midstream.
  1333. */
  1334. mutex_lock(&smack_ambient_lock);
  1335. asize = strlen(smack_net_ambient->smk_known) + 1;
  1336. if (cn >= asize)
  1337. rc = simple_read_from_buffer(buf, cn, ppos,
  1338. smack_net_ambient->smk_known,
  1339. asize);
  1340. else
  1341. rc = -EINVAL;
  1342. mutex_unlock(&smack_ambient_lock);
  1343. return rc;
  1344. }
  1345. /**
  1346. * smk_write_ambient - write() for /smack/ambient
  1347. * @file: file pointer, not actually used
  1348. * @buf: where to get the data from
  1349. * @count: bytes sent
  1350. * @ppos: where to start
  1351. *
  1352. * Returns number of bytes written or error code, as appropriate
  1353. */
  1354. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  1355. size_t count, loff_t *ppos)
  1356. {
  1357. struct smack_known *skp;
  1358. char *oldambient;
  1359. char *data;
  1360. int rc = count;
  1361. if (!smack_privileged(CAP_MAC_ADMIN))
  1362. return -EPERM;
  1363. data = kzalloc(count + 1, GFP_KERNEL);
  1364. if (data == NULL)
  1365. return -ENOMEM;
  1366. if (copy_from_user(data, buf, count) != 0) {
  1367. rc = -EFAULT;
  1368. goto out;
  1369. }
  1370. skp = smk_import_entry(data, count);
  1371. if (skp == NULL) {
  1372. rc = -EINVAL;
  1373. goto out;
  1374. }
  1375. mutex_lock(&smack_ambient_lock);
  1376. oldambient = smack_net_ambient->smk_known;
  1377. smack_net_ambient = skp;
  1378. smk_unlbl_ambient(oldambient);
  1379. mutex_unlock(&smack_ambient_lock);
  1380. out:
  1381. kfree(data);
  1382. return rc;
  1383. }
  1384. static const struct file_operations smk_ambient_ops = {
  1385. .read = smk_read_ambient,
  1386. .write = smk_write_ambient,
  1387. .llseek = default_llseek,
  1388. };
  1389. /**
  1390. * smk_read_onlycap - read() for /smack/onlycap
  1391. * @filp: file pointer, not actually used
  1392. * @buf: where to put the result
  1393. * @cn: maximum to send along
  1394. * @ppos: where to start
  1395. *
  1396. * Returns number of bytes read or error code, as appropriate
  1397. */
  1398. static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
  1399. size_t cn, loff_t *ppos)
  1400. {
  1401. char *smack = "";
  1402. ssize_t rc = -EINVAL;
  1403. int asize;
  1404. if (*ppos != 0)
  1405. return 0;
  1406. if (smack_onlycap != NULL)
  1407. smack = smack_onlycap;
  1408. asize = strlen(smack) + 1;
  1409. if (cn >= asize)
  1410. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  1411. return rc;
  1412. }
  1413. /**
  1414. * smk_write_onlycap - write() for /smack/onlycap
  1415. * @file: file pointer, not actually used
  1416. * @buf: where to get the data from
  1417. * @count: bytes sent
  1418. * @ppos: where to start
  1419. *
  1420. * Returns number of bytes written or error code, as appropriate
  1421. */
  1422. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  1423. size_t count, loff_t *ppos)
  1424. {
  1425. char *data;
  1426. struct smack_known *skp = smk_of_task(current->cred->security);
  1427. int rc = count;
  1428. if (!smack_privileged(CAP_MAC_ADMIN))
  1429. return -EPERM;
  1430. /*
  1431. * This can be done using smk_access() but is done
  1432. * explicitly for clarity. The smk_access() implementation
  1433. * would use smk_access(smack_onlycap, MAY_WRITE)
  1434. */
  1435. if (smack_onlycap != NULL && smack_onlycap != skp->smk_known)
  1436. return -EPERM;
  1437. data = kzalloc(count, GFP_KERNEL);
  1438. if (data == NULL)
  1439. return -ENOMEM;
  1440. /*
  1441. * Should the null string be passed in unset the onlycap value.
  1442. * This seems like something to be careful with as usually
  1443. * smk_import only expects to return NULL for errors. It
  1444. * is usually the case that a nullstring or "\n" would be
  1445. * bad to pass to smk_import but in fact this is useful here.
  1446. *
  1447. * smk_import will also reject a label beginning with '-',
  1448. * so "-usecapabilities" will also work.
  1449. */
  1450. if (copy_from_user(data, buf, count) != 0)
  1451. rc = -EFAULT;
  1452. else
  1453. smack_onlycap = smk_import(data, count);
  1454. kfree(data);
  1455. return rc;
  1456. }
  1457. static const struct file_operations smk_onlycap_ops = {
  1458. .read = smk_read_onlycap,
  1459. .write = smk_write_onlycap,
  1460. .llseek = default_llseek,
  1461. };
  1462. /**
  1463. * smk_read_logging - read() for /smack/logging
  1464. * @filp: file pointer, not actually used
  1465. * @buf: where to put the result
  1466. * @cn: maximum to send along
  1467. * @ppos: where to start
  1468. *
  1469. * Returns number of bytes read or error code, as appropriate
  1470. */
  1471. static ssize_t smk_read_logging(struct file *filp, char __user *buf,
  1472. size_t count, loff_t *ppos)
  1473. {
  1474. char temp[32];
  1475. ssize_t rc;
  1476. if (*ppos != 0)
  1477. return 0;
  1478. sprintf(temp, "%d\n", log_policy);
  1479. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1480. return rc;
  1481. }
  1482. /**
  1483. * smk_write_logging - write() for /smack/logging
  1484. * @file: file pointer, not actually used
  1485. * @buf: where to get the data from
  1486. * @count: bytes sent
  1487. * @ppos: where to start
  1488. *
  1489. * Returns number of bytes written or error code, as appropriate
  1490. */
  1491. static ssize_t smk_write_logging(struct file *file, const char __user *buf,
  1492. size_t count, loff_t *ppos)
  1493. {
  1494. char temp[32];
  1495. int i;
  1496. if (!smack_privileged(CAP_MAC_ADMIN))
  1497. return -EPERM;
  1498. if (count >= sizeof(temp) || count == 0)
  1499. return -EINVAL;
  1500. if (copy_from_user(temp, buf, count) != 0)
  1501. return -EFAULT;
  1502. temp[count] = '\0';
  1503. if (sscanf(temp, "%d", &i) != 1)
  1504. return -EINVAL;
  1505. if (i < 0 || i > 3)
  1506. return -EINVAL;
  1507. log_policy = i;
  1508. return count;
  1509. }
  1510. static const struct file_operations smk_logging_ops = {
  1511. .read = smk_read_logging,
  1512. .write = smk_write_logging,
  1513. .llseek = default_llseek,
  1514. };
  1515. /*
  1516. * Seq_file read operations for /smack/load-self
  1517. */
  1518. static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
  1519. {
  1520. struct task_smack *tsp = current_security();
  1521. return smk_seq_start(s, pos, &tsp->smk_rules);
  1522. }
  1523. static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1524. {
  1525. struct task_smack *tsp = current_security();
  1526. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1527. }
  1528. static int load_self_seq_show(struct seq_file *s, void *v)
  1529. {
  1530. struct list_head *list = v;
  1531. struct smack_rule *srp =
  1532. list_entry(list, struct smack_rule, list);
  1533. smk_rule_show(s, srp, SMK_LABELLEN);
  1534. return 0;
  1535. }
  1536. static const struct seq_operations load_self_seq_ops = {
  1537. .start = load_self_seq_start,
  1538. .next = load_self_seq_next,
  1539. .show = load_self_seq_show,
  1540. .stop = smk_seq_stop,
  1541. };
  1542. /**
  1543. * smk_open_load_self - open() for /smack/load-self2
  1544. * @inode: inode structure representing file
  1545. * @file: "load" file pointer
  1546. *
  1547. * For reading, use load_seq_* seq_file reading operations.
  1548. */
  1549. static int smk_open_load_self(struct inode *inode, struct file *file)
  1550. {
  1551. return seq_open(file, &load_self_seq_ops);
  1552. }
  1553. /**
  1554. * smk_write_load_self - write() for /smack/load-self
  1555. * @file: file pointer, not actually used
  1556. * @buf: where to get the data from
  1557. * @count: bytes sent
  1558. * @ppos: where to start - must be 0
  1559. *
  1560. */
  1561. static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
  1562. size_t count, loff_t *ppos)
  1563. {
  1564. struct task_smack *tsp = current_security();
  1565. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1566. &tsp->smk_rules_lock, SMK_FIXED24_FMT);
  1567. }
  1568. static const struct file_operations smk_load_self_ops = {
  1569. .open = smk_open_load_self,
  1570. .read = seq_read,
  1571. .llseek = seq_lseek,
  1572. .write = smk_write_load_self,
  1573. .release = seq_release,
  1574. };
  1575. /**
  1576. * smk_user_access - handle access check transaction
  1577. * @file: file pointer
  1578. * @buf: data from user space
  1579. * @count: bytes sent
  1580. * @ppos: where to start - must be 0
  1581. */
  1582. static ssize_t smk_user_access(struct file *file, const char __user *buf,
  1583. size_t count, loff_t *ppos, int format)
  1584. {
  1585. struct smack_parsed_rule rule;
  1586. char *data;
  1587. char *cod;
  1588. int res;
  1589. data = simple_transaction_get(file, buf, count);
  1590. if (IS_ERR(data))
  1591. return PTR_ERR(data);
  1592. if (format == SMK_FIXED24_FMT) {
  1593. if (count < SMK_LOADLEN)
  1594. return -EINVAL;
  1595. res = smk_parse_rule(data, &rule, 0);
  1596. } else {
  1597. /*
  1598. * Copy the data to make sure the string is terminated.
  1599. */
  1600. cod = kzalloc(count + 1, GFP_KERNEL);
  1601. if (cod == NULL)
  1602. return -ENOMEM;
  1603. memcpy(cod, data, count);
  1604. cod[count] = '\0';
  1605. res = smk_parse_long_rule(cod, &rule, 0, 0);
  1606. kfree(cod);
  1607. }
  1608. if (res)
  1609. return -EINVAL;
  1610. res = smk_access(rule.smk_subject, rule.smk_object,
  1611. rule.smk_access1, NULL);
  1612. data[0] = res == 0 ? '1' : '0';
  1613. data[1] = '\0';
  1614. simple_transaction_set(file, 2);
  1615. if (format == SMK_FIXED24_FMT)
  1616. return SMK_LOADLEN;
  1617. return count;
  1618. }
  1619. /**
  1620. * smk_write_access - handle access check transaction
  1621. * @file: file pointer
  1622. * @buf: data from user space
  1623. * @count: bytes sent
  1624. * @ppos: where to start - must be 0
  1625. */
  1626. static ssize_t smk_write_access(struct file *file, const char __user *buf,
  1627. size_t count, loff_t *ppos)
  1628. {
  1629. return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
  1630. }
  1631. static const struct file_operations smk_access_ops = {
  1632. .write = smk_write_access,
  1633. .read = simple_transaction_read,
  1634. .release = simple_transaction_release,
  1635. .llseek = generic_file_llseek,
  1636. };
  1637. /*
  1638. * Seq_file read operations for /smack/load2
  1639. */
  1640. static int load2_seq_show(struct seq_file *s, void *v)
  1641. {
  1642. struct list_head *list = v;
  1643. struct smack_master_list *smlp =
  1644. list_entry(list, struct smack_master_list, list);
  1645. smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
  1646. return 0;
  1647. }
  1648. static const struct seq_operations load2_seq_ops = {
  1649. .start = load2_seq_start,
  1650. .next = load2_seq_next,
  1651. .show = load2_seq_show,
  1652. .stop = smk_seq_stop,
  1653. };
  1654. /**
  1655. * smk_open_load2 - open() for /smack/load2
  1656. * @inode: inode structure representing file
  1657. * @file: "load2" file pointer
  1658. *
  1659. * For reading, use load2_seq_* seq_file reading operations.
  1660. */
  1661. static int smk_open_load2(struct inode *inode, struct file *file)
  1662. {
  1663. return seq_open(file, &load2_seq_ops);
  1664. }
  1665. /**
  1666. * smk_write_load2 - write() for /smack/load2
  1667. * @file: file pointer, not actually used
  1668. * @buf: where to get the data from
  1669. * @count: bytes sent
  1670. * @ppos: where to start - must be 0
  1671. *
  1672. */
  1673. static ssize_t smk_write_load2(struct file *file, const char __user *buf,
  1674. size_t count, loff_t *ppos)
  1675. {
  1676. /*
  1677. * Must have privilege.
  1678. */
  1679. if (!smack_privileged(CAP_MAC_ADMIN))
  1680. return -EPERM;
  1681. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  1682. SMK_LONG_FMT);
  1683. }
  1684. static const struct file_operations smk_load2_ops = {
  1685. .open = smk_open_load2,
  1686. .read = seq_read,
  1687. .llseek = seq_lseek,
  1688. .write = smk_write_load2,
  1689. .release = seq_release,
  1690. };
  1691. /*
  1692. * Seq_file read operations for /smack/load-self2
  1693. */
  1694. static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
  1695. {
  1696. struct task_smack *tsp = current_security();
  1697. return smk_seq_start(s, pos, &tsp->smk_rules);
  1698. }
  1699. static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1700. {
  1701. struct task_smack *tsp = current_security();
  1702. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1703. }
  1704. static int load_self2_seq_show(struct seq_file *s, void *v)
  1705. {
  1706. struct list_head *list = v;
  1707. struct smack_rule *srp =
  1708. list_entry(list, struct smack_rule, list);
  1709. smk_rule_show(s, srp, SMK_LONGLABEL);
  1710. return 0;
  1711. }
  1712. static const struct seq_operations load_self2_seq_ops = {
  1713. .start = load_self2_seq_start,
  1714. .next = load_self2_seq_next,
  1715. .show = load_self2_seq_show,
  1716. .stop = smk_seq_stop,
  1717. };
  1718. /**
  1719. * smk_open_load_self2 - open() for /smack/load-self2
  1720. * @inode: inode structure representing file
  1721. * @file: "load" file pointer
  1722. *
  1723. * For reading, use load_seq_* seq_file reading operations.
  1724. */
  1725. static int smk_open_load_self2(struct inode *inode, struct file *file)
  1726. {
  1727. return seq_open(file, &load_self2_seq_ops);
  1728. }
  1729. /**
  1730. * smk_write_load_self2 - write() for /smack/load-self2
  1731. * @file: file pointer, not actually used
  1732. * @buf: where to get the data from
  1733. * @count: bytes sent
  1734. * @ppos: where to start - must be 0
  1735. *
  1736. */
  1737. static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
  1738. size_t count, loff_t *ppos)
  1739. {
  1740. struct task_smack *tsp = current_security();
  1741. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1742. &tsp->smk_rules_lock, SMK_LONG_FMT);
  1743. }
  1744. static const struct file_operations smk_load_self2_ops = {
  1745. .open = smk_open_load_self2,
  1746. .read = seq_read,
  1747. .llseek = seq_lseek,
  1748. .write = smk_write_load_self2,
  1749. .release = seq_release,
  1750. };
  1751. /**
  1752. * smk_write_access2 - handle access check transaction
  1753. * @file: file pointer
  1754. * @buf: data from user space
  1755. * @count: bytes sent
  1756. * @ppos: where to start - must be 0
  1757. */
  1758. static ssize_t smk_write_access2(struct file *file, const char __user *buf,
  1759. size_t count, loff_t *ppos)
  1760. {
  1761. return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
  1762. }
  1763. static const struct file_operations smk_access2_ops = {
  1764. .write = smk_write_access2,
  1765. .read = simple_transaction_read,
  1766. .release = simple_transaction_release,
  1767. .llseek = generic_file_llseek,
  1768. };
  1769. /**
  1770. * smk_write_revoke_subj - write() for /smack/revoke-subject
  1771. * @file: file pointer
  1772. * @buf: data from user space
  1773. * @count: bytes sent
  1774. * @ppos: where to start - must be 0
  1775. */
  1776. static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
  1777. size_t count, loff_t *ppos)
  1778. {
  1779. char *data = NULL;
  1780. const char *cp = NULL;
  1781. struct smack_known *skp;
  1782. struct smack_rule *sp;
  1783. struct list_head *rule_list;
  1784. struct mutex *rule_lock;
  1785. int rc = count;
  1786. if (*ppos != 0)
  1787. return -EINVAL;
  1788. if (!smack_privileged(CAP_MAC_ADMIN))
  1789. return -EPERM;
  1790. if (count == 0 || count > SMK_LONGLABEL)
  1791. return -EINVAL;
  1792. data = kzalloc(count, GFP_KERNEL);
  1793. if (data == NULL)
  1794. return -ENOMEM;
  1795. if (copy_from_user(data, buf, count) != 0) {
  1796. rc = -EFAULT;
  1797. goto free_out;
  1798. }
  1799. cp = smk_parse_smack(data, count);
  1800. if (cp == NULL) {
  1801. rc = -EINVAL;
  1802. goto free_out;
  1803. }
  1804. skp = smk_find_entry(cp);
  1805. if (skp == NULL)
  1806. goto free_out;
  1807. rule_list = &skp->smk_rules;
  1808. rule_lock = &skp->smk_rules_lock;
  1809. mutex_lock(rule_lock);
  1810. list_for_each_entry_rcu(sp, rule_list, list)
  1811. sp->smk_access = 0;
  1812. mutex_unlock(rule_lock);
  1813. free_out:
  1814. kfree(data);
  1815. kfree(cp);
  1816. return rc;
  1817. }
  1818. static const struct file_operations smk_revoke_subj_ops = {
  1819. .write = smk_write_revoke_subj,
  1820. .read = simple_transaction_read,
  1821. .release = simple_transaction_release,
  1822. .llseek = generic_file_llseek,
  1823. };
  1824. static struct kset *smackfs_kset;
  1825. /**
  1826. * smk_init_sysfs - initialize /sys/fs/smackfs
  1827. *
  1828. */
  1829. static int smk_init_sysfs(void)
  1830. {
  1831. smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
  1832. if (!smackfs_kset)
  1833. return -ENOMEM;
  1834. return 0;
  1835. }
  1836. /**
  1837. * smk_write_change_rule - write() for /smack/change-rule
  1838. * @file: file pointer
  1839. * @buf: data from user space
  1840. * @count: bytes sent
  1841. * @ppos: where to start - must be 0
  1842. */
  1843. static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
  1844. size_t count, loff_t *ppos)
  1845. {
  1846. /*
  1847. * Must have privilege.
  1848. */
  1849. if (!capable(CAP_MAC_ADMIN))
  1850. return -EPERM;
  1851. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  1852. SMK_CHANGE_FMT);
  1853. }
  1854. static const struct file_operations smk_change_rule_ops = {
  1855. .write = smk_write_change_rule,
  1856. .read = simple_transaction_read,
  1857. .release = simple_transaction_release,
  1858. .llseek = generic_file_llseek,
  1859. };
  1860. /**
  1861. * smk_fill_super - fill the /smackfs superblock
  1862. * @sb: the empty superblock
  1863. * @data: unused
  1864. * @silent: unused
  1865. *
  1866. * Fill in the well known entries for /smack
  1867. *
  1868. * Returns 0 on success, an error code on failure
  1869. */
  1870. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  1871. {
  1872. int rc;
  1873. struct inode *root_inode;
  1874. static struct tree_descr smack_files[] = {
  1875. [SMK_LOAD] = {
  1876. "load", &smk_load_ops, S_IRUGO|S_IWUSR},
  1877. [SMK_CIPSO] = {
  1878. "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  1879. [SMK_DOI] = {
  1880. "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  1881. [SMK_DIRECT] = {
  1882. "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  1883. [SMK_AMBIENT] = {
  1884. "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  1885. [SMK_NETLBLADDR] = {
  1886. "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
  1887. [SMK_ONLYCAP] = {
  1888. "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  1889. [SMK_LOGGING] = {
  1890. "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
  1891. [SMK_LOAD_SELF] = {
  1892. "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
  1893. [SMK_ACCESSES] = {
  1894. "access", &smk_access_ops, S_IRUGO|S_IWUGO},
  1895. [SMK_MAPPED] = {
  1896. "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
  1897. [SMK_LOAD2] = {
  1898. "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
  1899. [SMK_LOAD_SELF2] = {
  1900. "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
  1901. [SMK_ACCESS2] = {
  1902. "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
  1903. [SMK_CIPSO2] = {
  1904. "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
  1905. [SMK_REVOKE_SUBJ] = {
  1906. "revoke-subject", &smk_revoke_subj_ops,
  1907. S_IRUGO|S_IWUSR},
  1908. [SMK_CHANGE_RULE] = {
  1909. "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
  1910. /* last one */
  1911. {""}
  1912. };
  1913. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  1914. if (rc != 0) {
  1915. printk(KERN_ERR "%s failed %d while creating inodes\n",
  1916. __func__, rc);
  1917. return rc;
  1918. }
  1919. root_inode = sb->s_root->d_inode;
  1920. return 0;
  1921. }
  1922. /**
  1923. * smk_mount - get the smackfs superblock
  1924. * @fs_type: passed along without comment
  1925. * @flags: passed along without comment
  1926. * @dev_name: passed along without comment
  1927. * @data: passed along without comment
  1928. *
  1929. * Just passes everything along.
  1930. *
  1931. * Returns what the lower level code does.
  1932. */
  1933. static struct dentry *smk_mount(struct file_system_type *fs_type,
  1934. int flags, const char *dev_name, void *data)
  1935. {
  1936. return mount_single(fs_type, flags, data, smk_fill_super);
  1937. }
  1938. static struct file_system_type smk_fs_type = {
  1939. .name = "smackfs",
  1940. .mount = smk_mount,
  1941. .kill_sb = kill_litter_super,
  1942. };
  1943. static struct vfsmount *smackfs_mount;
  1944. static int __init smk_preset_netlabel(struct smack_known *skp)
  1945. {
  1946. skp->smk_netlabel.domain = skp->smk_known;
  1947. skp->smk_netlabel.flags =
  1948. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  1949. return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  1950. &skp->smk_netlabel, strlen(skp->smk_known));
  1951. }
  1952. /**
  1953. * init_smk_fs - get the smackfs superblock
  1954. *
  1955. * register the smackfs
  1956. *
  1957. * Do not register smackfs if Smack wasn't enabled
  1958. * on boot. We can not put this method normally under the
  1959. * smack_init() code path since the security subsystem get
  1960. * initialized before the vfs caches.
  1961. *
  1962. * Returns true if we were not chosen on boot or if
  1963. * we were chosen and filesystem registration succeeded.
  1964. */
  1965. static int __init init_smk_fs(void)
  1966. {
  1967. int err;
  1968. int rc;
  1969. if (!security_module_enable(&smack_ops))
  1970. return 0;
  1971. err = smk_init_sysfs();
  1972. if (err)
  1973. printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
  1974. err = register_filesystem(&smk_fs_type);
  1975. if (!err) {
  1976. smackfs_mount = kern_mount(&smk_fs_type);
  1977. if (IS_ERR(smackfs_mount)) {
  1978. printk(KERN_ERR "smackfs: could not mount!\n");
  1979. err = PTR_ERR(smackfs_mount);
  1980. smackfs_mount = NULL;
  1981. }
  1982. }
  1983. smk_cipso_doi();
  1984. smk_unlbl_ambient(NULL);
  1985. rc = smk_preset_netlabel(&smack_known_floor);
  1986. if (err == 0 && rc < 0)
  1987. err = rc;
  1988. rc = smk_preset_netlabel(&smack_known_hat);
  1989. if (err == 0 && rc < 0)
  1990. err = rc;
  1991. rc = smk_preset_netlabel(&smack_known_huh);
  1992. if (err == 0 && rc < 0)
  1993. err = rc;
  1994. rc = smk_preset_netlabel(&smack_known_invalid);
  1995. if (err == 0 && rc < 0)
  1996. err = rc;
  1997. rc = smk_preset_netlabel(&smack_known_star);
  1998. if (err == 0 && rc < 0)
  1999. err = rc;
  2000. rc = smk_preset_netlabel(&smack_known_web);
  2001. if (err == 0 && rc < 0)
  2002. err = rc;
  2003. return err;
  2004. }
  2005. __initcall(init_smk_fs);