smackfs.c 61 KB

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