commoncap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /* Common capabilities, needed by capability.o and root_plug.o
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. */
  9. #include <linux/capability.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/security.h>
  14. #include <linux/file.h>
  15. #include <linux/mm.h>
  16. #include <linux/mman.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/swap.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/netlink.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/xattr.h>
  23. #include <linux/hugetlb.h>
  24. #include <linux/mount.h>
  25. #include <linux/sched.h>
  26. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  27. /*
  28. * Because of the reduced scope of CAP_SETPCAP when filesystem
  29. * capabilities are in effect, it is safe to allow this capability to
  30. * be available in the default configuration.
  31. */
  32. # define CAP_INIT_BSET CAP_FULL_SET
  33. #else /* ie. ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  34. # define CAP_INIT_BSET CAP_INIT_EFF_SET
  35. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  36. kernel_cap_t cap_bset = CAP_INIT_BSET; /* systemwide capability bound */
  37. EXPORT_SYMBOL(cap_bset);
  38. /* Global security state */
  39. unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
  40. EXPORT_SYMBOL(securebits);
  41. int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
  42. {
  43. NETLINK_CB(skb).eff_cap = current->cap_effective;
  44. return 0;
  45. }
  46. int cap_netlink_recv(struct sk_buff *skb, int cap)
  47. {
  48. if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
  49. return -EPERM;
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(cap_netlink_recv);
  53. int cap_capable (struct task_struct *tsk, int cap)
  54. {
  55. /* Derived from include/linux/sched.h:capable. */
  56. if (cap_raised(tsk->cap_effective, cap))
  57. return 0;
  58. return -EPERM;
  59. }
  60. int cap_settime(struct timespec *ts, struct timezone *tz)
  61. {
  62. if (!capable(CAP_SYS_TIME))
  63. return -EPERM;
  64. return 0;
  65. }
  66. int cap_ptrace (struct task_struct *parent, struct task_struct *child)
  67. {
  68. /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
  69. if (!cap_issubset(child->cap_permitted, parent->cap_permitted) &&
  70. !__capable(parent, CAP_SYS_PTRACE))
  71. return -EPERM;
  72. return 0;
  73. }
  74. int cap_capget (struct task_struct *target, kernel_cap_t *effective,
  75. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  76. {
  77. /* Derived from kernel/capability.c:sys_capget. */
  78. *effective = cap_t (target->cap_effective);
  79. *inheritable = cap_t (target->cap_inheritable);
  80. *permitted = cap_t (target->cap_permitted);
  81. return 0;
  82. }
  83. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  84. static inline int cap_block_setpcap(struct task_struct *target)
  85. {
  86. /*
  87. * No support for remote process capability manipulation with
  88. * filesystem capability support.
  89. */
  90. return (target != current);
  91. }
  92. static inline int cap_inh_is_capped(void)
  93. {
  94. /*
  95. * return 1 if changes to the inheritable set are limited
  96. * to the old permitted set.
  97. */
  98. return !cap_capable(current, CAP_SETPCAP);
  99. }
  100. #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  101. static inline int cap_block_setpcap(struct task_struct *t) { return 0; }
  102. static inline int cap_inh_is_capped(void) { return 1; }
  103. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  104. int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
  105. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  106. {
  107. if (cap_block_setpcap(target)) {
  108. return -EPERM;
  109. }
  110. if (cap_inh_is_capped()
  111. && !cap_issubset(*inheritable,
  112. cap_combine(target->cap_inheritable,
  113. current->cap_permitted))) {
  114. /* incapable of using this inheritable set */
  115. return -EPERM;
  116. }
  117. /* verify restrictions on target's new Permitted set */
  118. if (!cap_issubset (*permitted,
  119. cap_combine (target->cap_permitted,
  120. current->cap_permitted))) {
  121. return -EPERM;
  122. }
  123. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  124. if (!cap_issubset (*effective, *permitted)) {
  125. return -EPERM;
  126. }
  127. return 0;
  128. }
  129. void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
  130. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  131. {
  132. target->cap_effective = *effective;
  133. target->cap_inheritable = *inheritable;
  134. target->cap_permitted = *permitted;
  135. }
  136. static inline void bprm_clear_caps(struct linux_binprm *bprm)
  137. {
  138. cap_clear(bprm->cap_inheritable);
  139. cap_clear(bprm->cap_permitted);
  140. bprm->cap_effective = false;
  141. }
  142. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  143. int cap_inode_need_killpriv(struct dentry *dentry)
  144. {
  145. struct inode *inode = dentry->d_inode;
  146. int error;
  147. if (!inode->i_op || !inode->i_op->getxattr)
  148. return 0;
  149. error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  150. if (error <= 0)
  151. return 0;
  152. return 1;
  153. }
  154. int cap_inode_killpriv(struct dentry *dentry)
  155. {
  156. struct inode *inode = dentry->d_inode;
  157. if (!inode->i_op || !inode->i_op->removexattr)
  158. return 0;
  159. return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
  160. }
  161. static inline int cap_from_disk(__le32 *caps, struct linux_binprm *bprm,
  162. int size)
  163. {
  164. __u32 magic_etc;
  165. if (size != XATTR_CAPS_SZ)
  166. return -EINVAL;
  167. magic_etc = le32_to_cpu(caps[0]);
  168. switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
  169. case VFS_CAP_REVISION:
  170. if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  171. bprm->cap_effective = true;
  172. else
  173. bprm->cap_effective = false;
  174. bprm->cap_permitted = to_cap_t( le32_to_cpu(caps[1]) );
  175. bprm->cap_inheritable = to_cap_t( le32_to_cpu(caps[2]) );
  176. return 0;
  177. default:
  178. return -EINVAL;
  179. }
  180. }
  181. /* Locate any VFS capabilities: */
  182. static int get_file_caps(struct linux_binprm *bprm)
  183. {
  184. struct dentry *dentry;
  185. int rc = 0;
  186. __le32 v1caps[XATTR_CAPS_SZ];
  187. struct inode *inode;
  188. if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
  189. bprm_clear_caps(bprm);
  190. return 0;
  191. }
  192. dentry = dget(bprm->file->f_dentry);
  193. inode = dentry->d_inode;
  194. if (!inode->i_op || !inode->i_op->getxattr)
  195. goto out;
  196. rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &v1caps,
  197. XATTR_CAPS_SZ);
  198. if (rc == -ENODATA || rc == -EOPNOTSUPP) {
  199. /* no data, that's ok */
  200. rc = 0;
  201. goto out;
  202. }
  203. if (rc < 0)
  204. goto out;
  205. rc = cap_from_disk(v1caps, bprm, rc);
  206. if (rc)
  207. printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
  208. __FUNCTION__, rc, bprm->filename);
  209. out:
  210. dput(dentry);
  211. if (rc)
  212. bprm_clear_caps(bprm);
  213. return rc;
  214. }
  215. #else
  216. int cap_inode_need_killpriv(struct dentry *dentry)
  217. {
  218. return 0;
  219. }
  220. int cap_inode_killpriv(struct dentry *dentry)
  221. {
  222. return 0;
  223. }
  224. static inline int get_file_caps(struct linux_binprm *bprm)
  225. {
  226. bprm_clear_caps(bprm);
  227. return 0;
  228. }
  229. #endif
  230. int cap_bprm_set_security (struct linux_binprm *bprm)
  231. {
  232. int ret;
  233. ret = get_file_caps(bprm);
  234. if (ret)
  235. printk(KERN_NOTICE "%s: get_file_caps returned %d for %s\n",
  236. __FUNCTION__, ret, bprm->filename);
  237. /* To support inheritance of root-permissions and suid-root
  238. * executables under compatibility mode, we raise all three
  239. * capability sets for the file.
  240. *
  241. * If only the real uid is 0, we only raise the inheritable
  242. * and permitted sets of the executable file.
  243. */
  244. if (!issecure (SECURE_NOROOT)) {
  245. if (bprm->e_uid == 0 || current->uid == 0) {
  246. cap_set_full (bprm->cap_inheritable);
  247. cap_set_full (bprm->cap_permitted);
  248. }
  249. if (bprm->e_uid == 0)
  250. bprm->cap_effective = true;
  251. }
  252. return ret;
  253. }
  254. void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
  255. {
  256. /* Derived from fs/exec.c:compute_creds. */
  257. kernel_cap_t new_permitted, working;
  258. new_permitted = cap_intersect (bprm->cap_permitted, cap_bset);
  259. working = cap_intersect (bprm->cap_inheritable,
  260. current->cap_inheritable);
  261. new_permitted = cap_combine (new_permitted, working);
  262. if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
  263. !cap_issubset (new_permitted, current->cap_permitted)) {
  264. set_dumpable(current->mm, suid_dumpable);
  265. current->pdeath_signal = 0;
  266. if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  267. if (!capable(CAP_SETUID)) {
  268. bprm->e_uid = current->uid;
  269. bprm->e_gid = current->gid;
  270. }
  271. if (!capable (CAP_SETPCAP)) {
  272. new_permitted = cap_intersect (new_permitted,
  273. current->cap_permitted);
  274. }
  275. }
  276. }
  277. current->suid = current->euid = current->fsuid = bprm->e_uid;
  278. current->sgid = current->egid = current->fsgid = bprm->e_gid;
  279. /* For init, we want to retain the capabilities set
  280. * in the init_task struct. Thus we skip the usual
  281. * capability rules */
  282. if (!is_global_init(current)) {
  283. current->cap_permitted = new_permitted;
  284. current->cap_effective = bprm->cap_effective ?
  285. new_permitted : 0;
  286. }
  287. /* AUD: Audit candidate if current->cap_effective is set */
  288. current->keep_capabilities = 0;
  289. }
  290. int cap_bprm_secureexec (struct linux_binprm *bprm)
  291. {
  292. if (current->uid != 0) {
  293. if (bprm->cap_effective)
  294. return 1;
  295. if (!cap_isclear(bprm->cap_permitted))
  296. return 1;
  297. if (!cap_isclear(bprm->cap_inheritable))
  298. return 1;
  299. }
  300. return (current->euid != current->uid ||
  301. current->egid != current->gid);
  302. }
  303. int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
  304. size_t size, int flags)
  305. {
  306. if (!strcmp(name, XATTR_NAME_CAPS)) {
  307. if (!capable(CAP_SETFCAP))
  308. return -EPERM;
  309. return 0;
  310. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  311. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  312. !capable(CAP_SYS_ADMIN))
  313. return -EPERM;
  314. return 0;
  315. }
  316. int cap_inode_removexattr(struct dentry *dentry, char *name)
  317. {
  318. if (!strcmp(name, XATTR_NAME_CAPS)) {
  319. if (!capable(CAP_SETFCAP))
  320. return -EPERM;
  321. return 0;
  322. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  323. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  324. !capable(CAP_SYS_ADMIN))
  325. return -EPERM;
  326. return 0;
  327. }
  328. /* moved from kernel/sys.c. */
  329. /*
  330. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  331. * a process after a call to setuid, setreuid, or setresuid.
  332. *
  333. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  334. * {r,e,s}uid != 0, the permitted and effective capabilities are
  335. * cleared.
  336. *
  337. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  338. * capabilities of the process are cleared.
  339. *
  340. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  341. * capabilities are set to the permitted capabilities.
  342. *
  343. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  344. * never happen.
  345. *
  346. * -astor
  347. *
  348. * cevans - New behaviour, Oct '99
  349. * A process may, via prctl(), elect to keep its capabilities when it
  350. * calls setuid() and switches away from uid==0. Both permitted and
  351. * effective sets will be retained.
  352. * Without this change, it was impossible for a daemon to drop only some
  353. * of its privilege. The call to setuid(!=0) would drop all privileges!
  354. * Keeping uid 0 is not an option because uid 0 owns too many vital
  355. * files..
  356. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  357. */
  358. static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
  359. int old_suid)
  360. {
  361. if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
  362. (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
  363. !current->keep_capabilities) {
  364. cap_clear (current->cap_permitted);
  365. cap_clear (current->cap_effective);
  366. }
  367. if (old_euid == 0 && current->euid != 0) {
  368. cap_clear (current->cap_effective);
  369. }
  370. if (old_euid != 0 && current->euid == 0) {
  371. current->cap_effective = current->cap_permitted;
  372. }
  373. }
  374. int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
  375. int flags)
  376. {
  377. switch (flags) {
  378. case LSM_SETID_RE:
  379. case LSM_SETID_ID:
  380. case LSM_SETID_RES:
  381. /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
  382. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  383. cap_emulate_setxuid (old_ruid, old_euid, old_suid);
  384. }
  385. break;
  386. case LSM_SETID_FS:
  387. {
  388. uid_t old_fsuid = old_ruid;
  389. /* Copied from kernel/sys.c:setfsuid. */
  390. /*
  391. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  392. * if not, we might be a bit too harsh here.
  393. */
  394. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  395. if (old_fsuid == 0 && current->fsuid != 0) {
  396. cap_t (current->cap_effective) &=
  397. ~CAP_FS_MASK;
  398. }
  399. if (old_fsuid != 0 && current->fsuid == 0) {
  400. cap_t (current->cap_effective) |=
  401. (cap_t (current->cap_permitted) &
  402. CAP_FS_MASK);
  403. }
  404. }
  405. break;
  406. }
  407. default:
  408. return -EINVAL;
  409. }
  410. return 0;
  411. }
  412. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  413. /*
  414. * Rationale: code calling task_setscheduler, task_setioprio, and
  415. * task_setnice, assumes that
  416. * . if capable(cap_sys_nice), then those actions should be allowed
  417. * . if not capable(cap_sys_nice), but acting on your own processes,
  418. * then those actions should be allowed
  419. * This is insufficient now since you can call code without suid, but
  420. * yet with increased caps.
  421. * So we check for increased caps on the target process.
  422. */
  423. static inline int cap_safe_nice(struct task_struct *p)
  424. {
  425. if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
  426. !__capable(current, CAP_SYS_NICE))
  427. return -EPERM;
  428. return 0;
  429. }
  430. int cap_task_setscheduler (struct task_struct *p, int policy,
  431. struct sched_param *lp)
  432. {
  433. return cap_safe_nice(p);
  434. }
  435. int cap_task_setioprio (struct task_struct *p, int ioprio)
  436. {
  437. return cap_safe_nice(p);
  438. }
  439. int cap_task_setnice (struct task_struct *p, int nice)
  440. {
  441. return cap_safe_nice(p);
  442. }
  443. int cap_task_kill(struct task_struct *p, struct siginfo *info,
  444. int sig, u32 secid)
  445. {
  446. if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
  447. return 0;
  448. if (secid)
  449. /*
  450. * Signal sent as a particular user.
  451. * Capabilities are ignored. May be wrong, but it's the
  452. * only thing we can do at the moment.
  453. * Used only by usb drivers?
  454. */
  455. return 0;
  456. if (cap_issubset(p->cap_permitted, current->cap_permitted))
  457. return 0;
  458. if (capable(CAP_KILL))
  459. return 0;
  460. return -EPERM;
  461. }
  462. #else
  463. int cap_task_setscheduler (struct task_struct *p, int policy,
  464. struct sched_param *lp)
  465. {
  466. return 0;
  467. }
  468. int cap_task_setioprio (struct task_struct *p, int ioprio)
  469. {
  470. return 0;
  471. }
  472. int cap_task_setnice (struct task_struct *p, int nice)
  473. {
  474. return 0;
  475. }
  476. int cap_task_kill(struct task_struct *p, struct siginfo *info,
  477. int sig, u32 secid)
  478. {
  479. return 0;
  480. }
  481. #endif
  482. void cap_task_reparent_to_init (struct task_struct *p)
  483. {
  484. p->cap_effective = CAP_INIT_EFF_SET;
  485. p->cap_inheritable = CAP_INIT_INH_SET;
  486. p->cap_permitted = CAP_FULL_SET;
  487. p->keep_capabilities = 0;
  488. return;
  489. }
  490. int cap_syslog (int type)
  491. {
  492. if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
  493. return -EPERM;
  494. return 0;
  495. }
  496. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  497. {
  498. int cap_sys_admin = 0;
  499. if (cap_capable(current, CAP_SYS_ADMIN) == 0)
  500. cap_sys_admin = 1;
  501. return __vm_enough_memory(mm, pages, cap_sys_admin);
  502. }