smackfs.c 58 KB

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