commoncap.c 14 KB

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