smackfs.c 57 KB

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