smackfs.c 60 KB

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