graphene.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. /*
  2. * linux/graphene/graphene.c
  3. *
  4. * Copyright (C) 2013-, Chia-Che Tsai, Bhushan Jain and Donald Porter
  5. *
  6. * Manage the graphene information and security policies.
  7. */
  8. #include <linux/version.h>
  9. #include <linux/atomic.h>
  10. #include <linux/slab.h>
  11. #include <linux/sched.h>
  12. #include <linux/fs.h>
  13. #include <linux/file.h>
  14. #include <linux/fdtable.h>
  15. #include <linux/dcache.h>
  16. #include <linux/namei.h>
  17. #include <linux/fs_struct.h>
  18. #include <linux/mount.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/module.h>
  22. #include <linux/un.h>
  23. #include <linux/net.h>
  24. #include <net/sock.h>
  25. #include <net/inet_sock.h>
  26. #include <net/tcp_states.h>
  27. #include <linux/pipe_fs_i.h>
  28. #include <../fs/internal.h>
  29. #include <../security/apparmor/include/audit.h>
  30. #include "graphene.h"
  31. #include "graphene-ipc.h"
  32. static struct list_head unix_list = LIST_HEAD_INIT(unix_list);
  33. static DEFINE_SPINLOCK(unix_list_lock);
  34. static atomic_t gipc_session;
  35. static int add_graphene_unix(struct graphene_unix *u)
  36. {
  37. struct graphene_unix *tmp;
  38. int err = 0;
  39. rcu_read_lock();
  40. list_for_each_entry_rcu(tmp, &unix_list, list) {
  41. if (u->root.mnt) {
  42. if (!tmp->root.mnt)
  43. continue;
  44. if (!path_equal(&tmp->root, &u->root))
  45. continue;
  46. }
  47. if (u->prefix.len) {
  48. int len;
  49. if (!tmp->prefix.len)
  50. continue;
  51. len = u->prefix.len < tmp->prefix.len ?
  52. u->prefix.len : tmp->prefix.len;
  53. if (!strncmp(u->prefix.name, tmp->prefix.name, len)) {
  54. err = -EACCES;
  55. break;
  56. }
  57. }
  58. }
  59. if (!err) {
  60. spin_lock(&unix_list_lock);
  61. list_add_tail_rcu(&u->list, &unix_list);
  62. spin_unlock(&unix_list_lock);
  63. }
  64. rcu_read_unlock();
  65. return err;;
  66. }
  67. int dup_graphene_struct(struct task_struct *tsk)
  68. {
  69. struct graphene_struct *gs, *new;
  70. struct graphene_info *gi;
  71. if (!(tsk->graphene))
  72. return 0;
  73. if (tsk->group_leader != tsk) {
  74. atomic_inc(&tsk->graphene->g_count);
  75. return 0;
  76. }
  77. gs = tsk->graphene;
  78. new = kmalloc(sizeof(struct graphene_struct), GFP_KERNEL);
  79. if (!new)
  80. return -ENOMEM;
  81. spin_lock(&gs->g_lock);
  82. gi = gs->g_info;
  83. atomic_inc(&gi->gi_count);
  84. new->g_info = gi;
  85. spin_unlock(&gs->g_lock);
  86. atomic_set(&new->g_count, 1);
  87. spin_lock_init(&new->g_lock);
  88. tsk->graphene = new;
  89. return 0;
  90. }
  91. static void drop_graphene_info(struct graphene_info *info)
  92. {
  93. struct graphene_path *p, *n;
  94. int i;
  95. list_for_each_entry_safe(p, n, &info->gi_paths, list) {
  96. path_put(&p->path);
  97. kfree(p);
  98. }
  99. list_for_each_entry_safe(p, n, &info->gi_rpaths, list) {
  100. path_put(&p->path);
  101. kfree(p);
  102. }
  103. if (info->gi_libexec.dentry)
  104. path_put(&info->gi_libexec);
  105. if (info->gi_unix) {
  106. struct graphene_unix *u = info->gi_unix;
  107. if (!atomic_dec_return(&u->count)) {
  108. spin_lock(&unix_list_lock);
  109. if (!list_empty(&u->list)) {
  110. list_del_rcu(&u->list);
  111. spin_unlock(&unix_list_lock);
  112. synchronize_rcu();
  113. }
  114. if (u->root.mnt)
  115. path_put(&u->root);
  116. if (u->prefix.len)
  117. kfree(u->prefix.name);
  118. kfree(u);
  119. }
  120. }
  121. for (i = 0 ; i < 3 ; i++)
  122. if (info->gi_console[i].mnt)
  123. path_put(&info->gi_console[i]);
  124. kfree(info);
  125. }
  126. static void put_graphene_info(struct graphene_info *info)
  127. {
  128. if (!atomic_dec_return(&info->gi_count))
  129. drop_graphene_info(info);
  130. }
  131. void put_graphene_struct(struct task_struct *tsk)
  132. {
  133. struct graphene_struct *gs = tsk->graphene;
  134. if (gs) {
  135. tsk->graphene = NULL;
  136. if (atomic_dec_return(&gs->g_count))
  137. return;
  138. put_graphene_info(gs->g_info);
  139. kfree(gs);
  140. }
  141. }
  142. static inline
  143. struct graphene_info *get_graphene_info(struct graphene_struct *gs)
  144. {
  145. struct graphene_info *info;
  146. if (!gs)
  147. return NULL;
  148. rcu_read_lock();
  149. info = rcu_dereference_check(gs->g_info,
  150. lockdep_is_held(&gs->g_lock) ||
  151. atomic_read(&gs->g_count) == 1 ||
  152. rcu_my_thread_group_empty());
  153. rcu_read_unlock();
  154. return info;
  155. }
  156. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
  157. # define FILE_INODE(file) ((file)->f_inode)
  158. #else
  159. # define FILE_INODE(file) ((file)->f_dentry->d_inode)
  160. #endif
  161. static loff_t graphene_lib_llseek(struct file *file, loff_t offset, int origin)
  162. {
  163. struct inode *inode = FILE_INODE(file);
  164. if (!inode)
  165. return -EINVAL;
  166. if (!inode->i_fop || !inode->i_fop->llseek)
  167. return -EINVAL;
  168. return inode->i_fop->llseek(file, offset, origin);
  169. }
  170. static ssize_t graphene_lib_read (struct file *file, char __user *buf,
  171. size_t len, loff_t *ppos)
  172. {
  173. struct inode *inode = FILE_INODE(file);
  174. if (!inode)
  175. return -EINVAL;
  176. if (!inode->i_fop || !inode->i_fop->read)
  177. return -EINVAL;
  178. return inode->i_fop->read(file, buf, len, ppos);
  179. }
  180. static ssize_t graphene_lib_aio_read (struct kiocb *iocb, const struct iovec *iov,
  181. unsigned long nr_segs, loff_t pos)
  182. {
  183. struct inode *inode = FILE_INODE(iocb->ki_filp);
  184. if (!inode)
  185. return -EINVAL;
  186. if (!inode->i_fop || !inode->i_fop->aio_read)
  187. return -EINVAL;
  188. return inode->i_fop->aio_read(iocb, iov, nr_segs, pos);
  189. }
  190. static int graphene_lib_mmap(struct file *file, struct vm_area_struct *vma)
  191. {
  192. struct inode *inode = FILE_INODE(file);
  193. if (!inode)
  194. return -EINVAL;
  195. if (!inode->i_fop || !inode->i_fop->mmap)
  196. return -EINVAL;
  197. return inode->i_fop->mmap(file, vma);
  198. }
  199. static int graphene_lib_release(struct inode *inode, struct file *file)
  200. {
  201. if (!inode)
  202. return -EINVAL;
  203. if (!inode->i_fop || !inode->i_fop->release)
  204. return -EINVAL;
  205. return inode->i_fop->release(inode, file);
  206. }
  207. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
  208. # define DEFINE_PATH_BUFFER(fn, kpath, max) struct filename *fn; char *kpath; int max;
  209. #else
  210. # define DEFINE_PATH_BUFFER(fn, kpath, max) char * kpath; int max;
  211. #endif
  212. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
  213. # define GET_PATH_BUFFER(fn, kpath, max) \
  214. fn = __getname(); \
  215. kpath = (char *) fn + sizeof(*fn); \
  216. max = PATH_MAX - sizeof(*fn);
  217. #else
  218. # define GET_PATH_BUFFER(fn, kpath, max) \
  219. kpath = __getname(); \
  220. max = PATH_MAX;
  221. #endif
  222. #define DEFINE_PATH(dp, path, fn, kpath, max) \
  223. DEFINE_PATH_BUFFER(fn, kpath, max) \
  224. char *dp; \
  225. GET_PATH_BUFFER(fn, kpath, max) \
  226. dp = d_path(path, kpath, max);
  227. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
  228. # define PUT_PATH_BUFFER(fn, kpath) final_putname(fn);
  229. #else
  230. # define PUT_PATH_BUFFER(fn, kpath) putname(kpath);
  231. #endif
  232. static unsigned long
  233. graphene_lib_get_area(struct file *file, unsigned long addr, unsigned long len,
  234. unsigned long pgoff, unsigned long flags)
  235. {
  236. struct task_struct *current_tsk = current;
  237. struct graphene_info *gi = get_graphene_info(current_tsk->graphene);
  238. struct inode *inode = FILE_INODE(file);
  239. unsigned long (*get_area) (struct file *, unsigned long, unsigned long,
  240. unsigned long, unsigned long);
  241. if (!inode)
  242. return -EINVAL;
  243. if (gi->gi_libaddr) {
  244. if (!path_equal(&file->f_path, &gi->gi_libexec))
  245. BUG();
  246. if (!addr)
  247. addr = gi->gi_libaddr + pgoff * PAGE_SIZE;
  248. #ifdef CONFIG_GRAPHENE_DEBUG
  249. {
  250. DEFINE_PATH(dp, &file->f_path, fn, kpath, max)
  251. if (!IS_ERR(dp))
  252. printk(KERN_INFO "Graphene: PID %d MAP FILE %s"
  253. " OFF 0x%08lx AT 0x%016lx\n",
  254. current->pid, dp,
  255. pgoff * PAGE_SIZE, addr);
  256. PUT_PATH_BUFFER(fn, kpath)
  257. }
  258. #endif
  259. return addr;
  260. }
  261. get_area = (inode->i_fop && inode->i_fop->get_unmapped_area) ?
  262. inode->i_fop->get_unmapped_area :
  263. current_tsk->mm->get_unmapped_area;
  264. return get_area(file, addr, len, pgoff, flags);
  265. }
  266. /* These are file oprations required for execve */
  267. static struct file_operations graphene_lib_operations = {
  268. .llseek = graphene_lib_llseek,
  269. .read = graphene_lib_read,
  270. .aio_read = graphene_lib_aio_read,
  271. .mmap = graphene_lib_mmap,
  272. .get_unmapped_area = graphene_lib_get_area,
  273. .release = graphene_lib_release,
  274. };
  275. #ifdef CONFIG_GRAPHENE_DEBUG
  276. static void print_path(const char * fmt, struct path *path)
  277. {
  278. DEFINE_PATH(dp, path, fn, kpath, max)
  279. printk(fmt, current->pid, IS_ERR(dp) ? "(unknown)" : dp);
  280. PUT_PATH_BUFFER(fn, kpath)
  281. }
  282. #else
  283. # define print_path(...) do {} while (0)
  284. #endif
  285. int graphene_execve_open(struct file *file)
  286. {
  287. struct task_struct *current_tsk = current;
  288. struct graphene_info *gi = get_graphene_info(current_tsk->graphene);
  289. if (!current_tsk->in_execve)
  290. BUG();
  291. if (!path_equal(&file->f_path, &gi->gi_libexec)) {
  292. print_path(KERN_INFO "Graphene: DENY EXEC PID %d PATH %s\n",
  293. &file->f_path);
  294. return -EACCES;
  295. }
  296. if (!gi->gi_libaddr)
  297. goto accepted;
  298. file->f_op = &graphene_lib_operations;
  299. accepted:
  300. print_path(KERN_INFO "Graphene: ALLOW EXEC PID %d PATH %s\n",
  301. &file->f_path);
  302. return 0;
  303. }
  304. static int graphene_check_path(struct graphene_info *gi, int op, u32 mask,
  305. struct path *path, struct graphene_path *gp,
  306. int is_recursive)
  307. {
  308. if (!path_equal(path, &gp->path))
  309. return 0;
  310. if (mask & (MAY_READ|MAY_EXEC|MAY_ACCESS|
  311. AA_MAY_META_READ|AA_EXEC_MMAP|AA_MAY_LINK)) {
  312. if (!(gp->type & GRAPHENE_FS_READ))
  313. return -EACCES;
  314. }
  315. if (mask & (MAY_WRITE|MAY_APPEND|
  316. AA_MAY_CREATE|AA_MAY_DELETE|AA_MAY_META_WRITE|
  317. AA_MAY_CHMOD|AA_MAY_CHOWN)) {
  318. if (!(gp->type & GRAPHENE_FS_WRITE))
  319. return -EACCES;
  320. }
  321. return 1;
  322. }
  323. static int __common_perm(struct graphene_info *gi, int op, struct path *target,
  324. u32 mask)
  325. {
  326. struct graphene_path *p;
  327. struct path root, path = *target;
  328. struct qstr last;
  329. int rv = 0, i;
  330. BUG_ON(!path.dentry);
  331. path_get(&path);
  332. if (op == OP_OPEN) {
  333. int minor = iminor(path.dentry->d_inode);
  334. if (minor == GRAPHENE_MINOR)
  335. goto out;
  336. if (minor == GIPC_MINOR)
  337. goto out;
  338. }
  339. rcu_read_lock();
  340. for (i = 0 ; i < 3 ; i++) {
  341. if (!gi->gi_console[i].mnt)
  342. continue;
  343. if (path_equal(&gi->gi_console[i], &path))
  344. goto out;
  345. }
  346. list_for_each_entry_rcu(p, &gi->gi_paths, list) {
  347. rv = graphene_check_path(gi, op, mask, &path, p, 0);
  348. if (rv)
  349. goto out;
  350. }
  351. if (gi->gi_libexec.mnt && path_equal(&path, &gi->gi_libexec)) {
  352. rv = 0;
  353. goto out;
  354. }
  355. get_fs_root(current->fs, &root);
  356. last.len = 0;
  357. while (!path_equal(&path, &root)) {
  358. int is_recursive = 0;
  359. list_for_each_entry_rcu(p, &gi->gi_rpaths, list) {
  360. rv = graphene_check_path(gi, op, mask, &path, p,
  361. is_recursive);
  362. if (rv)
  363. goto out_root;
  364. }
  365. if (gi->gi_unix && gi->gi_unix->root.mnt) {
  366. struct graphene_unix *u = gi->gi_unix;
  367. if (path_equal(&path, &u->root)) {
  368. rv = 0;
  369. if (op == OP_MKNOD)
  370. goto out_root;
  371. if (op == OP_UNLINK) {
  372. if (!u->prefix.len)
  373. goto out_root;
  374. if (last.len) {
  375. int len = u->prefix.len;
  376. if (last.len < len)
  377. len = last.len;
  378. if (!strncmp(last.name,
  379. u->prefix.name,
  380. len))
  381. goto out_root;
  382. }
  383. }
  384. break;
  385. }
  386. }
  387. last = path.dentry->d_name;
  388. while(1) {
  389. struct dentry *old = path.dentry;
  390. if (path_equal(&path, &root))
  391. break;
  392. if (path.dentry != path.mnt->mnt_root) {
  393. path.dentry = dget_parent(path.dentry);
  394. dput(old);
  395. break;
  396. }
  397. if (!follow_up(&path))
  398. break;
  399. }
  400. is_recursive = 1;
  401. }
  402. rv = -EACCES;
  403. out_root:
  404. path_put(&root);
  405. out:
  406. rcu_read_unlock();
  407. path_put(&path);
  408. if (rv >= 0) {
  409. rv = 0;
  410. print_path(KERN_INFO "Graphene: ALLOW PID %d PATH %s\n", target);
  411. } else {
  412. print_path(KERN_INFO "Graphene: DENY PID %d PATH %s\n", target);
  413. }
  414. return rv;
  415. }
  416. int graphene_common_perm(int op, struct path *path, u32 mask)
  417. {
  418. struct graphene_info *gi = get_graphene_info(current->graphene);
  419. if (!gi)
  420. return 0;
  421. return __common_perm(gi, op, path, mask);
  422. }
  423. static int __unix_perm(struct sockaddr *address, int addrlen)
  424. {
  425. struct graphene_info *gi = get_graphene_info(current->graphene);
  426. const char *path, *sun_path;
  427. int err = 0, path_len;
  428. if (!gi->gi_unix)
  429. return -EPERM;
  430. path = sun_path = ((struct sockaddr_un *) address)->sun_path;
  431. if (gi->gi_unix->root.mnt) {
  432. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
  433. struct path parent;
  434. const char *s;
  435. err = kern_path(path, LOOKUP_PARENT, &parent);
  436. if (err)
  437. return err;
  438. if (!path_equal(&gi->gi_unix->root, &parent))
  439. goto denied;
  440. path_put(&parent);
  441. /* find the last name */
  442. for (s = path; *s ; s++)
  443. if (*s == '/')
  444. path = s + 1;
  445. path_len = s - path;
  446. #else
  447. struct nameidata nd;
  448. err = kern_path_parent(path, &nd);
  449. if (err)
  450. return err;
  451. path_put(&nd.path);
  452. path = nd.last.name;
  453. path_len = nd.last.len;
  454. if (!path_equal(&gi->gi_unix->root, &nd.path))
  455. goto denied;
  456. #endif
  457. } else {
  458. path_len = strlen(path);
  459. }
  460. if (gi->gi_unix->prefix.len) {
  461. if (gi->gi_unix->prefix.len > path_len ||
  462. memcmp(path,
  463. gi->gi_unix->prefix.name,
  464. gi->gi_unix->prefix.len))
  465. err = -EPERM;
  466. }
  467. if (!err)
  468. return 0;
  469. denied:
  470. #ifdef CONFIG_GRAPHENE_DEBUG
  471. printk(KERN_INFO "Graphene: DENY PID %d SOCKET %s\n",
  472. current->pid, sun_path);
  473. #endif
  474. return -EPERM;
  475. }
  476. static int net_cmp(int family, int addr_any, int port_any,
  477. struct graphene_net_addr *ga,
  478. struct sockaddr *addr, int addrlen)
  479. {
  480. switch(family) {
  481. case AF_INET: {
  482. struct sockaddr_in *a = (void *) addr;
  483. if (!addr_any) {
  484. if (a->sin_addr.s_addr != ga->addr.sin_addr.s_addr)
  485. return 1;
  486. }
  487. if (!port_any) {
  488. unsigned short port = ntohs(a->sin_port);
  489. if (!(port >= ga->port_begin && port <= ga->port_end))
  490. return 1;
  491. }
  492. break;
  493. }
  494. #ifdef CONFIG_IPV6
  495. case AF_INET6: {
  496. struct sockaddr_in6 *a6 = (void *) addr;
  497. if (!addr_any) {
  498. if (memcmp(&a6->sin6_addr, &ga->addr.sin6_addr,
  499. sizeof(struct in6_addr)))
  500. return 1;
  501. }
  502. if (!port_any) {
  503. unsigned short port = ntohs(a6->sin6_port);
  504. if (!(port >= ga->port_begin && port <= ga->port_end))
  505. return 1;
  506. }
  507. break;
  508. }
  509. #endif
  510. }
  511. return 0;
  512. }
  513. #ifdef CONFIG_GRAPHENE_DEBUG
  514. static void print_net(int allow, int family, int op,
  515. struct sockaddr *local_addr, int local_addrlen,
  516. struct sockaddr *peer_addr, int peer_addrlen)
  517. {
  518. const char *allow_str = allow ? "ALLOW" : "DENY";
  519. const char *op_str = "";
  520. int print_peer = (op == OP_CONNECT || op == OP_SENDMSG);
  521. switch(op) {
  522. case OP_BIND: op_str = "BIND"; break;
  523. case OP_LISTEN: op_str = "LISTEN"; break;
  524. case OP_CONNECT: op_str = "CONNECT"; break;
  525. case OP_SENDMSG: op_str = "SENDMSG"; break;
  526. case OP_RECVMSG: op_str = "RECVMSG"; break;
  527. }
  528. if (family == AF_INET) {
  529. struct sockaddr_in *la = (void *) local_addr;
  530. u8 *a1 = (u8 *) &la->sin_addr.s_addr;
  531. struct sockaddr_in *pa = (void *) peer_addr;
  532. u8 *a2 = (u8 *) &pa->sin_addr.s_addr;
  533. if (print_peer && peer_addr) {
  534. printk(KERN_INFO "Graphene: %s %s PID %d SOCKET "
  535. "%d.%d.%d.%d:%d:%d.%d.%d.%d:%d\n",
  536. allow_str, op_str, current->pid,
  537. a1[0], a1[1], a1[2], a1[3], ntohs(la->sin_port),
  538. a2[0], a2[1], a2[2], a2[3], ntohs(pa->sin_port));
  539. } else {
  540. printk(KERN_INFO "Graphene: %s %s PID %d SOCKET "
  541. "%d.%d.%d.%d:%d\n",
  542. allow_str, op_str, current->pid,
  543. a1[0], a1[1], a1[2], a1[3], ntohs(la->sin_port));
  544. }
  545. }
  546. #ifdef CONFIG_IPV6
  547. if (family == AF_INET6) {
  548. struct sockaddr_in6 *la = (void *) local_addr;
  549. u16 *a1 = (u16 *) &la->sin6_addr.s6_addr;
  550. struct sockaddr_in6 *pa = (void *) peer_addr;
  551. u16 *a2 = (u16 *) &pa->sin6_addr.s6_addr;
  552. if (print_peer) {
  553. printk(KERN_INFO "Graphene: %s %s PID %d SOCKET "
  554. "[%d:%d:%d:%d:%d:%d:%d:%d]:%d:"
  555. "[%d.%d.%d.%d:%d:%d:%d:%d]:%d\n",
  556. allow_str, op_str, current->pid,
  557. a1[0], a1[1], a1[2], a1[3],
  558. a1[4], a1[5], a1[6], a1[7], ntohs(la->sin6_port),
  559. a2[0], a2[1], a2[2], a2[3],
  560. a2[4], a2[5], a2[6], a2[7], ntohs(pa->sin6_port));
  561. } else {
  562. printk(KERN_INFO "Graphene: %s %s PID %d SOCKET "
  563. "[%d.%d.%d.%d:%d:%d:%d:%d]:%d\n",
  564. allow_str, op_str, current->pid,
  565. a1[0], a1[1], a1[2], a1[3],
  566. a1[4], a1[5], a1[6], a1[7], ntohs(la->sin6_port));
  567. }
  568. }
  569. #endif
  570. }
  571. #else
  572. # define print_net(...) do {} while (0)
  573. #endif
  574. /*
  575. * network rules:
  576. * bind:
  577. * input addr/port match local addr/port
  578. * listen:
  579. * local addr/port match local addr/port
  580. * allow ANY peer addr/port
  581. * connect:
  582. * local/remote addr/port match local/remote addr/port
  583. * sendmsg:
  584. * EITHER stream socket OR no inport addr/port OR
  585. * local/remote addr/port match local/remote addr/port
  586. * recvmsg:
  587. * EITHER stream socket OR connected OR
  588. * allow ANY peer addr/port
  589. */
  590. static
  591. int __common_net_perm(struct graphene_info *gi, int op, struct socket *sock,
  592. struct sockaddr *address, int addrlen)
  593. {
  594. struct sock *sk = sock->sk;
  595. struct inet_sock *inet = inet_sk(sk);
  596. struct graphene_net *gn;
  597. struct sockaddr_storage addrbuf;
  598. struct sockaddr * local_addr = NULL, * peer_addr = NULL;
  599. int local_addrlen, peer_addrlen;
  600. int local_needcmp = 0, peer_needcmp = 0;
  601. int local_needany = 0, peer_needany = 0;
  602. int err;
  603. if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM)
  604. return -EPERM;
  605. #ifdef CONFIG_IPV6
  606. if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
  607. #else
  608. if (sk->sk_family != AF_INET)
  609. #endif
  610. return -EPERM;
  611. if (list_empty(&gi->gi_net))
  612. return -EPERM;
  613. if (op == OP_LISTEN)
  614. peer_needany = 1;
  615. if (op == OP_RECVMSG) {
  616. if (inet->inet_dport)
  617. return 0;
  618. peer_needany = 1;
  619. }
  620. if (op == OP_CONNECT || op == OP_SENDMSG) {
  621. BUG_ON(!address);
  622. peer_addr = address;
  623. peer_addrlen = addrlen;
  624. peer_needcmp = 1;
  625. }
  626. if (op == OP_BIND) {
  627. BUG_ON(!address);
  628. local_addr = address;
  629. local_addrlen = addrlen;
  630. local_needcmp = 1;
  631. if (sk->sk_type == SOCK_DGRAM)
  632. peer_needany = 1;
  633. } else {
  634. local_addr = (struct sockaddr *) &addrbuf;
  635. local_needcmp = 1;
  636. err = sock->ops->getname(sock, local_addr, &local_addrlen, 0);
  637. if (err)
  638. return err;
  639. }
  640. list_for_each_entry(gn, &gi->gi_net, list) {
  641. if (gn->family != sk->sk_family)
  642. continue;
  643. if (local_needany &&
  644. (gn->flags & (LOCAL_ADDR_ANY|LOCAL_PORT_ANY)) !=
  645. (LOCAL_ADDR_ANY|LOCAL_PORT_ANY))
  646. continue;
  647. if (peer_needany &&
  648. (gn->flags & (PEER_ADDR_ANY|PEER_PORT_ANY)) !=
  649. (PEER_ADDR_ANY|PEER_PORT_ANY))
  650. continue;
  651. if (local_needcmp) {
  652. if (net_cmp(sk->sk_family, gn->flags & LOCAL_ADDR_ANY,
  653. gn->flags & LOCAL_PORT_ANY,
  654. &gn->local, local_addr, local_addrlen))
  655. continue;
  656. }
  657. if (peer_needcmp) {
  658. if (net_cmp(sk->sk_family, gn->flags & PEER_ADDR_ANY,
  659. gn->flags & PEER_PORT_ANY,
  660. &gn->peer, peer_addr, peer_addrlen))
  661. continue;
  662. }
  663. print_net(1, sk->sk_family, op, local_addr, local_addrlen,
  664. peer_addr, peer_addrlen);
  665. return 0;
  666. }
  667. print_net(0, sk->sk_family, op, local_addr, local_addrlen,
  668. peer_addr, peer_addrlen);
  669. return -EPERM;
  670. }
  671. int graphene_socket_bind(struct socket *sock,
  672. struct sockaddr *address, int addrlen)
  673. {
  674. if (GRAPHENE_ENABLED()) {
  675. struct graphene_info *gi = get_graphene_info(current->graphene);
  676. if (!sock || !sock->sk)
  677. return 0;
  678. if (sock->sk->sk_family == PF_UNIX) {
  679. if (sock->sk->sk_type != SOCK_STREAM)
  680. return -EPERM;
  681. return __unix_perm(address, addrlen);
  682. }
  683. return __common_net_perm(gi, OP_BIND, sock, address, addrlen);
  684. }
  685. return 0;
  686. }
  687. int graphene_socket_listen(struct socket *sock, int backlog)
  688. {
  689. if (GRAPHENE_ENABLED()) {
  690. struct graphene_info *gi = get_graphene_info(current->graphene);
  691. if (!sock || !sock->sk || sock->sk->sk_family == PF_UNIX)
  692. return 0;
  693. return __common_net_perm(gi, OP_LISTEN, sock, NULL, 0);
  694. }
  695. return 0;
  696. }
  697. int graphene_socket_connect(struct socket *sock,
  698. struct sockaddr *address, int addrlen)
  699. {
  700. if (GRAPHENE_ENABLED()) {
  701. struct graphene_info *gi = get_graphene_info(current->graphene);
  702. if (!sock || !sock->sk)
  703. return 0;
  704. if (sock->sk->sk_family == PF_UNIX) {
  705. if (sock->sk->sk_type != SOCK_STREAM)
  706. return -EPERM;
  707. return __unix_perm(address, addrlen);
  708. }
  709. return __common_net_perm(gi, OP_CONNECT, sock, address,
  710. addrlen);
  711. }
  712. return 0;
  713. }
  714. int graphene_socket_sendmsg(struct socket *sock,
  715. struct msghdr *msg, int size)
  716. {
  717. if (GRAPHENE_ENABLED()) {
  718. struct graphene_info *gi = get_graphene_info(current->graphene);
  719. if (!sock || !sock->sk || sock->sk->sk_family == PF_UNIX)
  720. return 0;
  721. if (sock->sk->sk_type == SOCK_STREAM)
  722. return 0;
  723. if (!msg->msg_name)
  724. return 0;
  725. return __common_net_perm(gi, OP_SENDMSG, sock,
  726. msg->msg_name, msg->msg_namelen);
  727. }
  728. return 0;
  729. }
  730. int graphene_socket_recvmsg(struct socket *sock,
  731. struct msghdr *msg, int size, int flags)
  732. {
  733. if (GRAPHENE_ENABLED()) {
  734. struct graphene_info *gi = get_graphene_info(current->graphene);
  735. if (!sock || !sock->sk || sock->sk->sk_family == PF_UNIX)
  736. return 0;
  737. if (sock->sk->sk_type == SOCK_STREAM)
  738. return 0;
  739. return __common_net_perm(gi, OP_RECVMSG, sock, NULL, 0);
  740. }
  741. return 0;
  742. }
  743. int graphene_task_kill(struct task_struct *tsk, struct siginfo *info,
  744. int sig, u32 secid)
  745. {
  746. struct task_struct *current_tsk = current;
  747. if (!current_tsk->graphene)
  748. return 0;
  749. if (sig != SIGCONT)
  750. return -EPERM;
  751. return (tsk->tgid == current_tsk->tgid) ? 0 : -EPERM;
  752. }
  753. static void get_console(struct graphene_info *gi, struct files_struct *files)
  754. {
  755. int i, j, n = 0;
  756. struct fdtable *fdt = files_fdtable(files);
  757. j = 0;
  758. rcu_read_lock();
  759. fdt = files_fdtable(files);
  760. rcu_read_unlock();
  761. for (;;) {
  762. unsigned long set;
  763. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
  764. i = j * BITS_PER_LONG;
  765. #else
  766. i = j * __NFDBITS;
  767. #endif
  768. if (i >= fdt->max_fds)
  769. break;
  770. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
  771. set = fdt->open_fds[j++];
  772. #else
  773. set = fdt->open_fds->fds_bits[j++];
  774. #endif
  775. for ( ; set ; i++, set >>= 1) {
  776. struct file *file;
  777. int k;
  778. if (!(set & 1))
  779. continue;
  780. if (i > 2)
  781. goto out;
  782. file = xchg(&fdt->fd[i], NULL);
  783. if (!file)
  784. continue;
  785. for (k = 0 ; k < n ; k++)
  786. if (path_equal(&file->f_path, &gi->gi_console[k]))
  787. break;
  788. if (k == n) {
  789. path_get(&file->f_path);
  790. gi->gi_console[n++] = file->f_path;
  791. }
  792. #ifdef CONFIG_GRAPHENE_DEBUG
  793. {
  794. DEFINE_PATH(dp, &file->f_path, fn, kpath, max)
  795. if (!IS_ERR(dp))
  796. printk(KERN_INFO "Graphene: "
  797. "PID %d CONSOLE %s\n",
  798. current->pid, dp);
  799. PUT_PATH_BUFFER(fn, kpath)
  800. }
  801. #endif
  802. xchg(&fdt->fd[i], file);
  803. }
  804. }
  805. out:
  806. for ( ; n < 3 ; n++)
  807. gi->gi_console[n].mnt = NULL;
  808. }
  809. static int update_graphene(struct task_struct *current_tsk,
  810. struct graphene_info *gi);
  811. #ifdef CONFIG_GRAPHENE_DEBUG
  812. static void print_net_rule(const char *fmt, struct graphene_net *n)
  813. {
  814. # ifdef CONFIG_IPV6
  815. # define ADDR_STR_MAX 128
  816. # else
  817. # define ADDR_STR_MAX 48
  818. # endif
  819. char str[ADDR_STR_MAX];
  820. int len = 0, i;
  821. for (i = 0; i < 2; i++) {
  822. unsigned char addr_any = i ? PEER_ADDR_ANY : LOCAL_ADDR_ANY;
  823. unsigned char port_any = i ? PEER_PORT_ANY : LOCAL_PORT_ANY;
  824. struct graphene_net_addr *a = i ? &n->peer : &n->local;
  825. if (i)
  826. str[len++] = ':';
  827. switch(n->family) {
  828. case AF_INET:
  829. if (n->flags & addr_any) {
  830. str[len++] = ':';
  831. } else {
  832. u8 *ip = (u8 *) &a->addr.sin_addr.s_addr;
  833. len += snprintf(str + len,
  834. ADDR_STR_MAX - len,
  835. "%u.%u.%u.%u:",
  836. ip[0], ip[1], ip[2], ip[3]);
  837. }
  838. break;
  839. #ifdef CONFIG_IPV6
  840. case AF_INET6:
  841. if (n->flags & addr_any) {
  842. str[len++] = '[';
  843. str[len++] = ']';
  844. str[len++] = ':';
  845. } else {
  846. u16 *ip = (u16 *) &a->addr.sin6_addr.s6_addr;
  847. len += snprintf(str + len,
  848. ADDR_STR_MAX - len,
  849. "[%u:%u:%u:%u:%u:%u:%u:%u]:",
  850. ip[0], ip[1], ip[2], ip[3],
  851. ip[4], ip[5], ip[6], ip[7]);
  852. }
  853. break;
  854. #endif /* CONFIG_IPV6 */
  855. }
  856. if (!(n->flags & port_any)) {
  857. if (a->port_begin == a->port_end)
  858. len += snprintf(str + len, ADDR_STR_MAX - len,
  859. "%u", a->port_begin);
  860. else
  861. len += snprintf(str + len, ADDR_STR_MAX - len,
  862. "%u-%u",
  863. a->port_begin, a->port_end);
  864. }
  865. }
  866. BUG_ON(len >= ADDR_STR_MAX);
  867. str[len] = 0;
  868. printk(fmt, current->pid, str);
  869. }
  870. #else
  871. # define print_net_rule(...) do {} while (0)
  872. #endif
  873. static int set_net_rule(struct graphene_net_policy *np,
  874. struct graphene_info *gi)
  875. {
  876. struct graphene_net *n;
  877. int i;
  878. #ifdef CONFIG_IPV6
  879. if (np->family != AF_INET && np->family != AF_INET6)
  880. #else
  881. if (np->family != AF_INET)
  882. #endif
  883. return -EINVAL;
  884. n = kmalloc(sizeof(struct graphene_net), GFP_KERNEL);
  885. if (!n)
  886. return -ENOMEM;
  887. n->family = np->family;
  888. n->flags = 0;
  889. n->local = np->local;
  890. n->peer = np->peer;
  891. for (i = 0; i < 2; i++) {
  892. unsigned char addr_any = i ? PEER_ADDR_ANY : LOCAL_ADDR_ANY;
  893. unsigned char port_any = i ? PEER_PORT_ANY : LOCAL_PORT_ANY;
  894. struct graphene_net_addr *a = i ? &n->peer : &n->local;
  895. switch(n->family) {
  896. case AF_INET:
  897. if (!a->addr.sin_addr.s_addr)
  898. n->flags |= addr_any;
  899. break;
  900. #ifdef CONFIG_IPV6
  901. case AF_INET6:
  902. if (!memcmp(&a->addr.sin6_addr.s6_addr, &in6addr_any, 16))
  903. n->flags |= addr_any;
  904. break;
  905. #endif /* CONFIG_IPV6 */
  906. }
  907. if (a->port_begin == 0 && a->port_end == 65535)
  908. n->flags |= port_any;
  909. }
  910. INIT_LIST_HEAD(&n->list);
  911. list_add_tail(&n->list, &gi->gi_net);
  912. print_net_rule(KERN_INFO "Graphene: PID %d NET RULE %s\n", n);
  913. return 0;
  914. }
  915. u32 gipc_get_session(struct task_struct *tsk)
  916. {
  917. struct graphene_info *gi = get_graphene_info(tsk->graphene);
  918. return gi ? gi->gi_gipc_session : 0;
  919. }
  920. int set_graphene(struct task_struct *current_tsk,
  921. const struct graphene_policies __user *gpolicies)
  922. {
  923. int npolicies;
  924. const struct graphene_user_policy __user *policies = gpolicies->policies;
  925. struct graphene_info *gi;
  926. struct graphene_user_policy ptmp;
  927. struct graphene_path *p;
  928. struct graphene_unix *u;
  929. int i, rv = 0;
  930. DEFINE_PATH_BUFFER(fn, kpath, max)
  931. #ifdef CONFIG_GRAPHENE_DEBUG
  932. char *dp;
  933. #endif
  934. rv = copy_from_user(&npolicies, &gpolicies->npolicies, sizeof(int));
  935. if (rv)
  936. return -EFAULT;
  937. if (npolicies && !policies)
  938. return -EINVAL;
  939. #ifndef CONFIG_GRAPHENE_ISOLATE
  940. if (current_tsk->graphene)
  941. return -EAGAIN;
  942. if (current_tsk != current_tsk->group_leader)
  943. return -EPERM;
  944. #endif
  945. gi = kmalloc(sizeof(struct graphene_info), GFP_KERNEL);
  946. if (!gi)
  947. return -ENOMEM;
  948. GET_PATH_BUFFER(fn, kpath, max)
  949. memset(gi, 0, sizeof(struct graphene_info));
  950. INIT_LIST_HEAD(&gi->gi_paths);
  951. INIT_LIST_HEAD(&gi->gi_rpaths);
  952. INIT_LIST_HEAD(&gi->gi_net);
  953. gi->gi_gipc_session = atomic_inc_return(&gipc_session);
  954. #ifdef CONFIG_GRAPHENE_DEBUG
  955. printk(KERN_INFO "Graphene: PID %d GIPC SESSION %u\n",
  956. current_tsk->pid, gi->gi_gipc_session);
  957. #endif
  958. for (i = 0 ; i < npolicies ; i++) {
  959. int type, flags;
  960. rv = copy_from_user(&ptmp, policies + i,
  961. sizeof(struct graphene_user_policy));
  962. if (rv) {
  963. rv = -EFAULT;
  964. goto err;
  965. }
  966. if (!ptmp.value) {
  967. rv = -EINVAL;
  968. goto err;
  969. }
  970. type = ptmp.type & ~(GRAPHENE_FS_READ | GRAPHENE_FS_WRITE);
  971. flags = ptmp.type & ~type;
  972. switch(type) {
  973. case GRAPHENE_LIB_NAME:
  974. rv = strncpy_from_user(kpath, ptmp.value, max);
  975. if (rv < 0)
  976. goto err;
  977. rv = kern_path(kpath, LOOKUP_FOLLOW, &gi->gi_libexec);
  978. if (rv)
  979. goto err;
  980. #ifdef CONFIG_GRAPHENE_DEBUG
  981. dp = d_path(&gi->gi_libexec, kpath, max);
  982. if (IS_ERR(dp)) {
  983. rv = -EINVAL;
  984. goto err;
  985. }
  986. printk(KERN_INFO "Graphene: PID %d LIB NAME %s\n",
  987. current_tsk->pid, dp);
  988. #endif
  989. break;
  990. case GRAPHENE_LIB_ADDR:
  991. gi->gi_libaddr = (u64) ptmp.value;
  992. #ifdef CONFIG_GRAPHENE_DEBUG
  993. printk(KERN_INFO "Graphene: PID %d LIB ADDR 0x%016llx\n",
  994. current_tsk->pid, gi->gi_libaddr);
  995. #endif
  996. break;
  997. case GRAPHENE_UNIX_ROOT:
  998. rv = strncpy_from_user(kpath, ptmp.value, max);
  999. if (rv < 0)
  1000. goto err;
  1001. u = gi->gi_unix;
  1002. if (!u) {
  1003. u = kmalloc(sizeof(struct graphene_unix),
  1004. GFP_KERNEL);
  1005. if (!u) {
  1006. rv = -ENOMEM;
  1007. goto err;
  1008. }
  1009. u->root.mnt = NULL;
  1010. u->prefix.len = 0;
  1011. atomic_set(&u->count, 1);
  1012. INIT_LIST_HEAD(&u->list);
  1013. gi->gi_unix = u;
  1014. }
  1015. if (u && u->root.mnt)
  1016. path_put(&u->root);
  1017. rv = kern_path(kpath, LOOKUP_FOLLOW, &u->root);
  1018. if (rv)
  1019. goto err;
  1020. #ifdef CONFIG_GRAPHENE_DEBUG
  1021. dp = d_path(&u->root, kpath, max);
  1022. if (IS_ERR(dp)) {
  1023. rv = -EINVAL;
  1024. goto err;
  1025. }
  1026. printk(KERN_INFO "Graphene: PID %d UNIX ROOT %s\n",
  1027. current_tsk->pid, dp);
  1028. #endif
  1029. break;
  1030. case GRAPHENE_UNIX_PREFIX: {
  1031. char * prefix;
  1032. rv = strncpy_from_user(kpath, ptmp.value, max);
  1033. if (rv < 0)
  1034. goto err;
  1035. u = gi->gi_unix;
  1036. if (!u) {
  1037. u = kmalloc(sizeof(struct graphene_unix),
  1038. GFP_KERNEL);
  1039. if (!u) {
  1040. rv = -ENOMEM;
  1041. goto err;
  1042. }
  1043. u->root.mnt = NULL;
  1044. u->prefix.len = 0;
  1045. atomic_set(&u->count, 1);
  1046. INIT_LIST_HEAD(&u->list);
  1047. gi->gi_unix = u;
  1048. }
  1049. if (u && u->prefix.len)
  1050. kfree(&u->prefix.name);
  1051. prefix = kmalloc(rv + 1, GFP_KERNEL);
  1052. if (!prefix) {
  1053. rv = -ENOMEM;
  1054. goto err;
  1055. }
  1056. memcpy(prefix, kpath, rv + 1);
  1057. u->prefix.len = rv;
  1058. u->prefix.name = prefix;
  1059. #ifdef CONFIG_GRAPHENE_DEBUG
  1060. printk(KERN_INFO "Graphene: PID %d UNIX PREFIX %s\n",
  1061. current_tsk->pid, kpath);
  1062. #endif
  1063. break;
  1064. }
  1065. case GRAPHENE_NET_RULE: {
  1066. struct graphene_net_policy np;
  1067. rv = copy_from_user(&np, ptmp.value,
  1068. sizeof(struct graphene_net_policy));
  1069. if (rv) {
  1070. rv = -EFAULT;
  1071. goto err;
  1072. }
  1073. rv = set_net_rule(&np, gi);
  1074. if (rv < 0)
  1075. goto err;
  1076. break;
  1077. }
  1078. case GRAPHENE_FS_PATH:
  1079. case GRAPHENE_FS_RECURSIVE:
  1080. rv = strncpy_from_user(kpath, ptmp.value, max);
  1081. if (rv < 0)
  1082. goto err;
  1083. p = kmalloc(sizeof(struct graphene_path),
  1084. GFP_KERNEL);
  1085. if (!p) {
  1086. rv = -ENOMEM;
  1087. goto err;
  1088. }
  1089. rv = kern_path(kpath, LOOKUP_FOLLOW, &p->path);
  1090. if (rv) {
  1091. kfree(p);
  1092. goto err;
  1093. }
  1094. #ifdef CONFIG_GRAPHENE_DEBUG
  1095. dp = d_path(&p->path, kpath, max);
  1096. if (IS_ERR(dp)) {
  1097. rv = -EINVAL;
  1098. kfree(p);
  1099. goto err;
  1100. }
  1101. printk(KERN_INFO "Graphene: PID %d PATH %s%s\n",
  1102. current_tsk->pid, dp,
  1103. type == GRAPHENE_FS_PATH ? "" :
  1104. " (recursive)");
  1105. #endif
  1106. p->type = flags;
  1107. INIT_LIST_HEAD(&p->list);
  1108. list_add_tail(&p->list,
  1109. type == GRAPHENE_FS_PATH ?
  1110. &gi->gi_paths : &gi->gi_rpaths);
  1111. break;
  1112. }
  1113. }
  1114. if (!current_tsk->graphene) {
  1115. struct graphene_struct *gs;
  1116. if (gi->gi_unix) {
  1117. rv = add_graphene_unix(gi->gi_unix);
  1118. if (rv < 0)
  1119. goto err;
  1120. }
  1121. get_console(gi, current_tsk->files);
  1122. gs = kmalloc(sizeof(struct graphene_struct), GFP_KERNEL);
  1123. if (!gs) {
  1124. rv = -ENOMEM;
  1125. goto err;
  1126. }
  1127. atomic_set(&gs->g_count, 1);
  1128. gs->g_info = gi;
  1129. spin_lock_init(&gs->g_lock);
  1130. current_tsk->graphene = gs;
  1131. printk(KERN_INFO "Graphene: PID %d registered\n",
  1132. current_tsk->pid);
  1133. }
  1134. #ifdef CONFIG_GRAPHENE_ISOLATE
  1135. else {
  1136. if ((rv = update_graphene(current_tsk, gi)) < 0) {
  1137. printk(KERN_INFO "Graphene: PID %d cannot be updated (%d)\n",
  1138. current_tsk->pid, rv);
  1139. goto err;
  1140. }
  1141. printk(KERN_INFO "Graphene: PID %d updated\n",
  1142. current_tsk->pid);
  1143. }
  1144. #endif
  1145. rv = 0;
  1146. goto out;
  1147. err:
  1148. drop_graphene_info(gi);
  1149. out:
  1150. PUT_PATH_BUFFER(fn, kpath)
  1151. return rv;
  1152. }
  1153. #ifdef CONFIG_GRAPHENE_ISOLATE
  1154. static int do_close_sock(struct graphene_info *gi, struct socket *sock,
  1155. int close_unix)
  1156. {
  1157. struct sock *sk = sock->sk;
  1158. struct sockaddr_storage address;
  1159. struct sockaddr *addr = (void *) &address;
  1160. struct inet_sock *inet;
  1161. int len, err;
  1162. if (!sk)
  1163. return 0;
  1164. if (sk->sk_family == PF_UNIX)
  1165. return close_unix ? -EPERM : 0;
  1166. inet = inet_sk(sk);
  1167. if (inet->inet_dport) {
  1168. err = sock->ops->getname(sock, addr, &len, 1);
  1169. if (err)
  1170. return err;
  1171. return __common_net_perm(gi, OP_CONNECT, sock, addr, len);
  1172. }
  1173. if (!inet->inet_num)
  1174. return 0;
  1175. if (sk->sk_state == TCP_LISTEN) {
  1176. err = __common_net_perm(gi, OP_LISTEN, sock, NULL, 0);
  1177. } else {
  1178. err = sock->ops->getname(sock, addr, &len, 0);
  1179. if (err)
  1180. return err;
  1181. err = __common_net_perm(gi, OP_BIND, sock, addr, len);
  1182. }
  1183. return err;
  1184. }
  1185. static int do_close_fds(struct graphene_info *gi, struct files_struct *files,
  1186. int close_unix)
  1187. {
  1188. int i, j;
  1189. struct fdtable *fdt = files_fdtable(files);
  1190. j = 0;
  1191. rcu_read_lock();
  1192. fdt = files_fdtable(files);
  1193. rcu_read_unlock();
  1194. for (;;) {
  1195. unsigned long set;
  1196. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
  1197. i = j * BITS_PER_LONG;
  1198. #else
  1199. i = j * __NFDBITS;
  1200. #endif
  1201. if (i >= fdt->max_fds)
  1202. break;
  1203. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
  1204. set = fdt->open_fds[j++];
  1205. #else
  1206. set = fdt->open_fds->fds_bits[j++];
  1207. #endif
  1208. for ( ; set ; i++, set >>= 1) {
  1209. struct socket *sock = NULL;
  1210. struct file *file;
  1211. int err;
  1212. if (!(set & 1))
  1213. continue;
  1214. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
  1215. sock = sockfd_lookup(i, &err);
  1216. #endif
  1217. file = xchg(&fdt->fd[i], NULL);
  1218. if (!file)
  1219. continue;
  1220. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
  1221. sock = sock_from_file(file, &err);
  1222. #endif
  1223. if (sock) {
  1224. err = do_close_sock(gi, sock, close_unix);
  1225. if (!err)
  1226. goto allow;
  1227. goto deny;
  1228. }
  1229. if (get_pipe_info(file))
  1230. goto deny;
  1231. err = __common_perm(gi, OP_OPEN, &file->f_path,
  1232. aa_map_file_to_perms(file));
  1233. if (!err) {
  1234. allow:
  1235. xchg(&fdt->fd[i], file);
  1236. continue;
  1237. }
  1238. deny:
  1239. filp_close(file, files);
  1240. cond_resched();
  1241. }
  1242. }
  1243. return 0;
  1244. }
  1245. static
  1246. int net_check (int family, int addr_any, int port_any,
  1247. int flags1, struct graphene_net_addr * addr1,
  1248. int flags2, struct graphene_net_addr * addr2)
  1249. {
  1250. if (flags2 & addr_any)
  1251. goto port;
  1252. if (flags1 & addr_any)
  1253. goto port;
  1254. switch (family) {
  1255. case AF_INET:
  1256. if (memcmp(&addr1->addr.sin_addr,
  1257. &addr2->addr.sin_addr,
  1258. sizeof(struct in_addr)))
  1259. return -EACCES;
  1260. break;
  1261. case AF_INET6:
  1262. if (memcmp(&addr1->addr.sin6_addr,
  1263. &addr2->addr.sin6_addr,
  1264. sizeof(struct in6_addr)))
  1265. return -EACCES;
  1266. break;
  1267. }
  1268. port:
  1269. if (flags2 & port_any)
  1270. return 0;
  1271. if (flags1 & port_any)
  1272. return 0;
  1273. if (addr1->port_begin < addr2->port_begin ||
  1274. addr1->port_end > addr2->port_end)
  1275. return -EACCES;
  1276. return 0;
  1277. }
  1278. static int update_graphene(struct task_struct *current_tsk,
  1279. struct graphene_info *new)
  1280. {
  1281. struct graphene_struct *gs = current_tsk->graphene;
  1282. struct graphene_info *gi = get_graphene_info(gs);
  1283. struct graphene_path *p;
  1284. struct graphene_net *n1, *n2;
  1285. int i = 0, close_unix = 0;
  1286. if (new->gi_unix) {
  1287. if (!new->gi_unix->root.mnt &&
  1288. gi->gi_unix && gi->gi_unix->root.mnt) {
  1289. if (!path_equal(&new->gi_unix->root,
  1290. &gi->gi_unix->root))
  1291. return -EACCES;
  1292. path_get(&gi->gi_unix->root);
  1293. new->gi_unix->root = gi->gi_unix->root;
  1294. }
  1295. if (new->gi_unix->prefix.len) {
  1296. int err = add_graphene_unix(new->gi_unix);
  1297. if (err)
  1298. return err;
  1299. }
  1300. close_unix = 1;
  1301. }
  1302. for (i = 0 ; i < 3 ; i++)
  1303. if (gi->gi_console[i].mnt) {
  1304. path_get(&gi->gi_console[i]);
  1305. new->gi_console[i] = gi->gi_console[i];
  1306. } else {
  1307. new->gi_console[i].mnt = NULL;
  1308. }
  1309. list_for_each_entry(p, &new->gi_paths, list) {
  1310. u32 mask = 0;
  1311. if (p->type & GRAPHENE_FS_READ)
  1312. mask |= MAY_READ;
  1313. if (p->type & GRAPHENE_FS_WRITE)
  1314. mask |= MAY_WRITE;
  1315. print_path(KERN_INFO "Graphene: PID %d CHECK RULE %s\n",
  1316. &p->path);
  1317. if (__common_perm(gi, OP_OPEN, &p->path, mask) < 0)
  1318. return -EACCES;
  1319. }
  1320. list_for_each_entry(n1, &new->gi_net, list) {
  1321. bool accepted = false;
  1322. print_net_rule(KERN_INFO "Graphene: PID %d CHECK RULE %s\n",
  1323. n1);
  1324. list_for_each_entry(n2, &gi->gi_net, list) {
  1325. if (n1->family != n2->family)
  1326. continue;
  1327. if (net_check(n1->family,
  1328. LOCAL_ADDR_ANY, LOCAL_PORT_ANY,
  1329. n1->flags, &n1->local,
  1330. n2->flags, &n2->local) < 0)
  1331. continue;
  1332. if (net_check(n1->family,
  1333. PEER_ADDR_ANY, PEER_PORT_ANY,
  1334. n1->flags, &n1->peer,
  1335. n2->flags, &n2->peer) < 0)
  1336. continue;
  1337. accepted = true;
  1338. print_net_rule(KERN_INFO "Graphene: PID %d ALLOW %s\n",
  1339. n1);
  1340. break;
  1341. }
  1342. if (!accepted) {
  1343. print_net_rule(KERN_INFO "Graphene: PID %d DENY %s\n",
  1344. n1);
  1345. return -EACCES;
  1346. }
  1347. }
  1348. spin_lock(&gs->g_lock);
  1349. put_graphene_info(gs->g_info);
  1350. gs->g_info = new;
  1351. spin_unlock(&gs->g_lock);
  1352. do_close_fds(new, current_tsk->files, close_unix);
  1353. return 0;
  1354. }
  1355. #endif /* CONFIG_GRAPHENE_ISOLATE */
  1356. static long graphene_ioctl(struct file *file, unsigned int cmd,
  1357. unsigned long arg)
  1358. {
  1359. struct task_struct *current_tsk = current;
  1360. switch (cmd) {
  1361. case GRAPHENE_SET_TASK:
  1362. return set_graphene(current_tsk,
  1363. (const struct graphene_policies __user *) arg);
  1364. default:
  1365. return -ENOSYS;
  1366. }
  1367. }
  1368. static struct file_operations graphene_operations = {
  1369. .unlocked_ioctl = graphene_ioctl,
  1370. .compat_ioctl = graphene_ioctl,
  1371. .llseek = noop_llseek,
  1372. };
  1373. static struct miscdevice graphene_dev = {
  1374. .minor = GRAPHENE_MINOR,
  1375. .name = "graphene",
  1376. .fops = &graphene_operations,
  1377. .mode = 0666,
  1378. };
  1379. static int __init graphene_init(void)
  1380. {
  1381. int rv;
  1382. rv = misc_register(&graphene_dev);
  1383. if (rv) {
  1384. printk(KERN_ERR "Graphene error: "
  1385. "failed to add a char device (rv=%d)\n", rv);
  1386. return rv;
  1387. }
  1388. return 0;
  1389. }
  1390. device_initcall(graphene_init);