cred.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Task credentials management - see Documentation/security/credentials.rst
  3. *
  4. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #include <linux/export.h>
  8. #include <linux/cred.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched.h>
  11. #include <linux/sched/coredump.h>
  12. #include <linux/key.h>
  13. #include <linux/keyctl.h>
  14. #include <linux/init_task.h>
  15. #include <linux/security.h>
  16. #include <linux/binfmts.h>
  17. #include <linux/cn_proc.h>
  18. #include <linux/uidgid.h>
  19. #include <trace/hooks/creds.h>
  20. #ifdef CONFIG_KDP_CRED
  21. #include <linux/kdp.h>
  22. #endif
  23. #if 0
  24. #define kdebug(FMT, ...) \
  25. printk("[%-5.5s%5u] " FMT "\n", \
  26. current->comm, current->pid, ##__VA_ARGS__)
  27. #else
  28. #define kdebug(FMT, ...) \
  29. do { \
  30. if (0) \
  31. no_printk("[%-5.5s%5u] " FMT "\n", \
  32. current->comm, current->pid, ##__VA_ARGS__); \
  33. } while (0)
  34. #endif
  35. static struct kmem_cache *cred_jar;
  36. /* init to 2 - one for init_task, one to ensure it is never freed */
  37. static struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  38. /*
  39. * The initial credentials for the initial task
  40. */
  41. struct cred init_cred
  42. #ifdef CONFIG_KDP_CRED
  43. __kdp_ro
  44. #endif
  45. = {
  46. .usage = ATOMIC_INIT(4),
  47. #ifdef CONFIG_DEBUG_CREDENTIALS
  48. .subscribers = ATOMIC_INIT(2),
  49. .magic = CRED_MAGIC,
  50. #endif
  51. .uid = GLOBAL_ROOT_UID,
  52. .gid = GLOBAL_ROOT_GID,
  53. .suid = GLOBAL_ROOT_UID,
  54. .sgid = GLOBAL_ROOT_GID,
  55. .euid = GLOBAL_ROOT_UID,
  56. .egid = GLOBAL_ROOT_GID,
  57. .fsuid = GLOBAL_ROOT_UID,
  58. .fsgid = GLOBAL_ROOT_GID,
  59. .securebits = SECUREBITS_DEFAULT,
  60. .cap_inheritable = CAP_EMPTY_SET,
  61. .cap_permitted = CAP_FULL_SET,
  62. .cap_effective = CAP_FULL_SET,
  63. .cap_bset = CAP_FULL_SET,
  64. .user = INIT_USER,
  65. .user_ns = &init_user_ns,
  66. .group_info = &init_groups,
  67. .ucounts = &init_ucounts,
  68. };
  69. static inline void set_cred_subscribers(struct cred *cred, int n)
  70. {
  71. #ifdef CONFIG_DEBUG_CREDENTIALS
  72. atomic_set(&cred->subscribers, n);
  73. #endif
  74. }
  75. static inline int read_cred_subscribers(const struct cred *cred)
  76. {
  77. #ifdef CONFIG_DEBUG_CREDENTIALS
  78. return atomic_read(&cred->subscribers);
  79. #else
  80. return 0;
  81. #endif
  82. }
  83. static inline void alter_cred_subscribers(const struct cred *_cred, int n)
  84. {
  85. #ifdef CONFIG_DEBUG_CREDENTIALS
  86. struct cred *cred = (struct cred *) _cred;
  87. atomic_add(n, &cred->subscribers);
  88. #endif
  89. }
  90. /*
  91. * The RCU callback to actually dispose of a set of credentials
  92. */
  93. static void put_cred_rcu(struct rcu_head *rcu)
  94. {
  95. struct cred *cred = container_of(rcu, struct cred, rcu);
  96. kdebug("put_cred_rcu(%p)", cred);
  97. #ifdef CONFIG_DEBUG_CREDENTIALS
  98. if (cred->magic != CRED_MAGIC_DEAD ||
  99. atomic_read(&cred->usage) != 0 ||
  100. read_cred_subscribers(cred) != 0)
  101. panic("CRED: put_cred_rcu() sees %p with"
  102. " mag %x, put %p, usage %d, subscr %d\n",
  103. cred, cred->magic, cred->put_addr,
  104. atomic_read(&cred->usage),
  105. read_cred_subscribers(cred));
  106. #else
  107. if (atomic_read(&cred->usage) != 0)
  108. panic("CRED: put_cred_rcu() sees %p with usage %d\n",
  109. cred, atomic_read(&cred->usage));
  110. #endif
  111. security_cred_free(cred);
  112. key_put(cred->session_keyring);
  113. key_put(cred->process_keyring);
  114. key_put(cred->thread_keyring);
  115. key_put(cred->request_key_auth);
  116. if (cred->group_info)
  117. put_group_info(cred->group_info);
  118. free_uid(cred->user);
  119. if (cred->ucounts)
  120. put_ucounts(cred->ucounts);
  121. put_user_ns(cred->user_ns);
  122. kmem_cache_free(cred_jar, cred);
  123. }
  124. /**
  125. * __put_cred - Destroy a set of credentials
  126. * @cred: The record to release
  127. *
  128. * Destroy a set of credentials on which no references remain.
  129. */
  130. void __put_cred(struct cred *cred)
  131. {
  132. kdebug("__put_cred(%p{%d,%d})", cred,
  133. atomic_read(&cred->usage),
  134. read_cred_subscribers(cred));
  135. #ifdef CONFIG_KDP_CRED
  136. BUG_ON(kdp_get_usecount(cred) != 0);
  137. #else
  138. BUG_ON(atomic_read(&cred->usage) != 0);
  139. #endif
  140. #ifdef CONFIG_DEBUG_CREDENTIALS
  141. BUG_ON(read_cred_subscribers(cred) != 0);
  142. cred->magic = CRED_MAGIC_DEAD;
  143. cred->put_addr = __builtin_return_address(0);
  144. #endif
  145. #ifdef CONFIG_KDP_CRED
  146. if(cred == current->cred)
  147. printk("[KDP] cred->security: 0x%lx\n", (unsigned long) cred->security);
  148. #endif
  149. BUG_ON(cred == current->cred);
  150. BUG_ON(cred == current->real_cred);
  151. #ifdef CONFIG_KDP_CRED
  152. kdp_put_cred_rcu(cred, (void *)put_cred_rcu);
  153. #else
  154. if (cred->non_rcu)
  155. put_cred_rcu(&cred->rcu);
  156. else
  157. call_rcu(&cred->rcu, put_cred_rcu);
  158. #endif
  159. }
  160. EXPORT_SYMBOL(__put_cred);
  161. /*
  162. * Clean up a task's credentials when it exits
  163. */
  164. void exit_creds(struct task_struct *tsk)
  165. {
  166. struct cred *cred;
  167. kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
  168. atomic_read(&tsk->cred->usage),
  169. read_cred_subscribers(tsk->cred));
  170. cred = (struct cred *) tsk->real_cred;
  171. tsk->real_cred = NULL;
  172. validate_creds(cred);
  173. alter_cred_subscribers(cred, -1);
  174. put_cred(cred);
  175. cred = (struct cred *) tsk->cred;
  176. tsk->cred = NULL;
  177. validate_creds(cred);
  178. alter_cred_subscribers(cred, -1);
  179. put_cred(cred);
  180. #ifdef CONFIG_KEYS_REQUEST_CACHE
  181. key_put(tsk->cached_requested_key);
  182. tsk->cached_requested_key = NULL;
  183. #endif
  184. trace_android_rvh_exit_creds(tsk, cred);
  185. }
  186. /**
  187. * get_task_cred - Get another task's objective credentials
  188. * @task: The task to query
  189. *
  190. * Get the objective credentials of a task, pinning them so that they can't go
  191. * away. Accessing a task's credentials directly is not permitted.
  192. *
  193. * The caller must also make sure task doesn't get deleted, either by holding a
  194. * ref on task or by holding tasklist_lock to prevent it from being unlinked.
  195. */
  196. const struct cred *get_task_cred(struct task_struct *task)
  197. {
  198. const struct cred *cred;
  199. rcu_read_lock();
  200. do {
  201. cred = __task_cred((task));
  202. BUG_ON(!cred);
  203. } while (!get_cred_rcu(cred));
  204. rcu_read_unlock();
  205. return cred;
  206. }
  207. EXPORT_SYMBOL(get_task_cred);
  208. /*
  209. * Allocate blank credentials, such that the credentials can be filled in at a
  210. * later date without risk of ENOMEM.
  211. */
  212. struct cred *cred_alloc_blank(void)
  213. {
  214. struct cred *new;
  215. new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
  216. if (!new)
  217. return NULL;
  218. atomic_set(&new->usage, 1);
  219. #ifdef CONFIG_DEBUG_CREDENTIALS
  220. new->magic = CRED_MAGIC;
  221. #endif
  222. if (security_cred_alloc_blank(new, GFP_KERNEL_ACCOUNT) < 0)
  223. goto error;
  224. return new;
  225. error:
  226. abort_creds(new);
  227. return NULL;
  228. }
  229. /**
  230. * prepare_creds - Prepare a new set of credentials for modification
  231. *
  232. * Prepare a new set of task credentials for modification. A task's creds
  233. * shouldn't generally be modified directly, therefore this function is used to
  234. * prepare a new copy, which the caller then modifies and then commits by
  235. * calling commit_creds().
  236. *
  237. * Preparation involves making a copy of the objective creds for modification.
  238. *
  239. * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
  240. *
  241. * Call commit_creds() or abort_creds() to clean up.
  242. */
  243. struct cred *prepare_creds(void)
  244. {
  245. struct task_struct *task = current;
  246. const struct cred *old;
  247. struct cred *new;
  248. validate_process_creds();
  249. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  250. if (!new)
  251. return NULL;
  252. kdebug("prepare_creds() alloc %p", new);
  253. old = task->cred;
  254. memcpy(new, old, sizeof(struct cred));
  255. new->non_rcu = 0;
  256. atomic_set(&new->usage, 1);
  257. set_cred_subscribers(new, 0);
  258. get_group_info(new->group_info);
  259. get_uid(new->user);
  260. get_user_ns(new->user_ns);
  261. #ifdef CONFIG_KEYS
  262. key_get(new->session_keyring);
  263. key_get(new->process_keyring);
  264. key_get(new->thread_keyring);
  265. key_get(new->request_key_auth);
  266. #endif
  267. #ifdef CONFIG_SECURITY
  268. new->security = NULL;
  269. #endif
  270. new->ucounts = get_ucounts(new->ucounts);
  271. if (!new->ucounts)
  272. goto error;
  273. if (security_prepare_creds(new, old, GFP_KERNEL_ACCOUNT) < 0)
  274. goto error;
  275. validate_creds(new);
  276. return new;
  277. error:
  278. abort_creds(new);
  279. return NULL;
  280. }
  281. EXPORT_SYMBOL(prepare_creds);
  282. /*
  283. * Prepare credentials for current to perform an execve()
  284. * - The caller must hold ->cred_guard_mutex
  285. */
  286. struct cred *prepare_exec_creds(void)
  287. {
  288. struct cred *new;
  289. new = prepare_creds();
  290. if (!new)
  291. return new;
  292. #ifdef CONFIG_KEYS
  293. /* newly exec'd tasks don't get a thread keyring */
  294. key_put(new->thread_keyring);
  295. new->thread_keyring = NULL;
  296. /* inherit the session keyring; new process keyring */
  297. key_put(new->process_keyring);
  298. new->process_keyring = NULL;
  299. #endif
  300. new->suid = new->fsuid = new->euid;
  301. new->sgid = new->fsgid = new->egid;
  302. return new;
  303. }
  304. /*
  305. * Copy credentials for the new process created by fork()
  306. *
  307. * We share if we can, but under some circumstances we have to generate a new
  308. * set.
  309. *
  310. * The new process gets the current process's subjective credentials as its
  311. * objective and subjective credentials
  312. */
  313. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  314. {
  315. struct cred *new;
  316. int ret;
  317. #ifdef CONFIG_KEYS_REQUEST_CACHE
  318. p->cached_requested_key = NULL;
  319. #endif
  320. #ifdef CONFIG_KDP_CRED
  321. if (!kdp_enable){
  322. #endif
  323. if (
  324. #ifdef CONFIG_KEYS
  325. !p->cred->thread_keyring &&
  326. #endif
  327. clone_flags & CLONE_THREAD
  328. ) {
  329. p->real_cred = get_cred(p->cred);
  330. get_cred(p->cred);
  331. alter_cred_subscribers(p->cred, 2);
  332. kdebug("share_creds(%p{%d,%d})",
  333. p->cred, atomic_read(&p->cred->usage),
  334. read_cred_subscribers(p->cred));
  335. inc_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
  336. return 0;
  337. }
  338. #ifdef CONFIG_KDP_CRED
  339. }
  340. #endif
  341. new = prepare_creds();
  342. if (!new)
  343. return -ENOMEM;
  344. if (clone_flags & CLONE_NEWUSER) {
  345. ret = create_user_ns(new);
  346. if (ret < 0)
  347. goto error_put;
  348. ret = set_cred_ucounts(new);
  349. if (ret < 0)
  350. goto error_put;
  351. }
  352. #ifdef CONFIG_KEYS
  353. /* new threads get their own thread keyrings if their parent already
  354. * had one */
  355. if (new->thread_keyring) {
  356. key_put(new->thread_keyring);
  357. new->thread_keyring = NULL;
  358. if (clone_flags & CLONE_THREAD)
  359. install_thread_keyring_to_cred(new);
  360. }
  361. /* The process keyring is only shared between the threads in a process;
  362. * anything outside of those threads doesn't inherit.
  363. */
  364. if (!(clone_flags & CLONE_THREAD)) {
  365. key_put(new->process_keyring);
  366. new->process_keyring = NULL;
  367. }
  368. #endif
  369. #ifdef CONFIG_KDP_CRED
  370. if (kdp_enable) {
  371. p->cred = p->real_cred = prepare_ro_creds(new, CMD_COPY_CREDS, (u64)p);
  372. inc_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
  373. put_cred(new);
  374. } else {
  375. p->cred = p->real_cred = get_cred(new);
  376. inc_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
  377. alter_cred_subscribers(new, 2);
  378. validate_creds(new);
  379. }
  380. #else
  381. p->cred = p->real_cred = get_cred(new);
  382. inc_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
  383. alter_cred_subscribers(new, 2);
  384. validate_creds(new);
  385. #endif
  386. return 0;
  387. error_put:
  388. put_cred(new);
  389. return ret;
  390. }
  391. static bool cred_cap_issubset(const struct cred *set, const struct cred *subset)
  392. {
  393. const struct user_namespace *set_ns = set->user_ns;
  394. const struct user_namespace *subset_ns = subset->user_ns;
  395. /* If the two credentials are in the same user namespace see if
  396. * the capabilities of subset are a subset of set.
  397. */
  398. if (set_ns == subset_ns)
  399. return cap_issubset(subset->cap_permitted, set->cap_permitted);
  400. /* The credentials are in a different user namespaces
  401. * therefore one is a subset of the other only if a set is an
  402. * ancestor of subset and set->euid is owner of subset or one
  403. * of subsets ancestors.
  404. */
  405. for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
  406. if ((set_ns == subset_ns->parent) &&
  407. uid_eq(subset_ns->owner, set->euid))
  408. return true;
  409. }
  410. return false;
  411. }
  412. /**
  413. * commit_creds - Install new credentials upon the current task
  414. * @new: The credentials to be assigned
  415. *
  416. * Install a new set of credentials to the current task, using RCU to replace
  417. * the old set. Both the objective and the subjective credentials pointers are
  418. * updated. This function may not be called if the subjective credentials are
  419. * in an overridden state.
  420. *
  421. * This function eats the caller's reference to the new credentials.
  422. *
  423. * Always returns 0 thus allowing this function to be tail-called at the end
  424. * of, say, sys_setgid().
  425. */
  426. int commit_creds(struct cred *new)
  427. {
  428. struct task_struct *task = current;
  429. const struct cred *old = task->real_cred;
  430. kdebug("commit_creds(%p{%d,%d})", new,
  431. atomic_read(&new->usage),
  432. read_cred_subscribers(new));
  433. BUG_ON(task->cred != old);
  434. #ifdef CONFIG_DEBUG_CREDENTIALS
  435. BUG_ON(read_cred_subscribers(old) < 2);
  436. validate_creds(old);
  437. validate_creds(new);
  438. #endif
  439. #ifdef CONFIG_KDP_CRED
  440. BUG_ON(kdp_get_usecount(new) < 1);
  441. #else
  442. BUG_ON(atomic_read(&new->usage) < 1);
  443. #endif
  444. get_cred(new); /* we will require a ref for the subj creds too */
  445. /* dumpability changes */
  446. if (!uid_eq(old->euid, new->euid) ||
  447. !gid_eq(old->egid, new->egid) ||
  448. !uid_eq(old->fsuid, new->fsuid) ||
  449. !gid_eq(old->fsgid, new->fsgid) ||
  450. !cred_cap_issubset(old, new)) {
  451. if (task->mm)
  452. set_dumpable(task->mm, suid_dumpable);
  453. task->pdeath_signal = 0;
  454. /*
  455. * If a task drops privileges and becomes nondumpable,
  456. * the dumpability change must become visible before
  457. * the credential change; otherwise, a __ptrace_may_access()
  458. * racing with this change may be able to attach to a task it
  459. * shouldn't be able to attach to (as if the task had dropped
  460. * privileges without becoming nondumpable).
  461. * Pairs with a read barrier in __ptrace_may_access().
  462. */
  463. smp_wmb();
  464. }
  465. /* alter the thread keyring */
  466. if (!uid_eq(new->fsuid, old->fsuid))
  467. key_fsuid_changed(new);
  468. if (!gid_eq(new->fsgid, old->fsgid))
  469. key_fsgid_changed(new);
  470. /* do it
  471. * RLIMIT_NPROC limits on user->processes have already been checked
  472. * in set_user().
  473. */
  474. alter_cred_subscribers(new, 2);
  475. if (new->user != old->user || new->user_ns != old->user_ns)
  476. inc_rlimit_ucounts(new->ucounts, UCOUNT_RLIMIT_NPROC, 1);
  477. #ifdef CONFIG_KDP_CRED
  478. if (kdp_enable) {
  479. struct cred *new_ro;
  480. new_ro = prepare_ro_creds(new, CMD_COMMIT_CREDS, 0);
  481. rcu_assign_pointer(task->real_cred, new_ro);
  482. rcu_assign_pointer(task->cred, new_ro);
  483. } else {
  484. rcu_assign_pointer(task->real_cred, new);
  485. rcu_assign_pointer(task->cred, new);
  486. }
  487. #else
  488. rcu_assign_pointer(task->real_cred, new);
  489. rcu_assign_pointer(task->cred, new);
  490. #endif
  491. trace_android_rvh_commit_creds(task, new);
  492. if (new->user != old->user || new->user_ns != old->user_ns)
  493. dec_rlimit_ucounts(old->ucounts, UCOUNT_RLIMIT_NPROC, 1);
  494. alter_cred_subscribers(old, -2);
  495. /* send notifications */
  496. if (!uid_eq(new->uid, old->uid) ||
  497. !uid_eq(new->euid, old->euid) ||
  498. !uid_eq(new->suid, old->suid) ||
  499. !uid_eq(new->fsuid, old->fsuid))
  500. proc_id_connector(task, PROC_EVENT_UID);
  501. if (!gid_eq(new->gid, old->gid) ||
  502. !gid_eq(new->egid, old->egid) ||
  503. !gid_eq(new->sgid, old->sgid) ||
  504. !gid_eq(new->fsgid, old->fsgid))
  505. proc_id_connector(task, PROC_EVENT_GID);
  506. #ifdef CONFIG_KDP_CRED
  507. if (kdp_enable) {
  508. put_cred(new);
  509. put_cred(new);
  510. }
  511. #endif
  512. /* release the old obj and subj refs both */
  513. put_cred(old);
  514. put_cred(old);
  515. return 0;
  516. }
  517. EXPORT_SYMBOL(commit_creds);
  518. /**
  519. * abort_creds - Discard a set of credentials and unlock the current task
  520. * @new: The credentials that were going to be applied
  521. *
  522. * Discard a set of credentials that were under construction and unlock the
  523. * current task.
  524. */
  525. void abort_creds(struct cred *new)
  526. {
  527. #ifdef CONFIG_KDP_CRED
  528. int ret = 0;
  529. #endif
  530. kdebug("abort_creds(%p{%d,%d})", new,
  531. atomic_read(&new->usage),
  532. read_cred_subscribers(new));
  533. #ifdef CONFIG_DEBUG_CREDENTIALS
  534. BUG_ON(read_cred_subscribers(new) != 0);
  535. #endif
  536. #ifdef CONFIG_KDP_CRED
  537. ret = is_kdp_protect_addr((unsigned long)new);
  538. if (ret == PROTECT_INIT)
  539. BUG_ON(atomic_read(init_cred_kdp.use_cnt) < 1);
  540. else if (ret == PROTECT_KMEM)
  541. BUG_ON(atomic_read(((struct cred_kdp *)new)->use_cnt) < 1);
  542. else
  543. #endif
  544. BUG_ON(atomic_read(&new->usage) < 1);
  545. put_cred(new);
  546. }
  547. EXPORT_SYMBOL(abort_creds);
  548. /**
  549. * override_creds - Override the current process's subjective credentials
  550. * @new: The credentials to be assigned
  551. *
  552. * Install a set of temporary override subjective credentials on the current
  553. * process, returning the old set for later reversion.
  554. */
  555. const struct cred *override_creds(const struct cred *new)
  556. {
  557. const struct cred *old = current->cred;
  558. kdebug("override_creds(%p{%d,%d})", new,
  559. atomic_read(&new->usage),
  560. read_cred_subscribers(new));
  561. validate_creds(old);
  562. validate_creds(new);
  563. /*
  564. * NOTE! This uses 'get_new_cred()' rather than 'get_cred()'.
  565. *
  566. * That means that we do not clear the 'non_rcu' flag, since
  567. * we are only installing the cred into the thread-synchronous
  568. * '->cred' pointer, not the '->real_cred' pointer that is
  569. * visible to other threads under RCU.
  570. *
  571. * Also note that we did validate_creds() manually, not depending
  572. * on the validation in 'get_cred()'.
  573. */
  574. get_new_cred((struct cred *)new);
  575. alter_cred_subscribers(new, 1);
  576. #ifdef CONFIG_KDP_CRED
  577. if (kdp_enable) {
  578. volatile unsigned int kdp_use_count = kdp_get_usecount((struct cred *)new);
  579. struct cred *new_ro;
  580. new_ro = prepare_ro_creds((struct cred *)new, CMD_OVRD_CREDS, kdp_use_count);
  581. GET_ROCRED_RCU(new_ro)->reflected_cred = (void *)new;
  582. rcu_assign_pointer(current->cred, new_ro);
  583. } else
  584. #endif
  585. rcu_assign_pointer(current->cred, new);
  586. trace_android_rvh_override_creds(current, new);
  587. alter_cred_subscribers(old, -1);
  588. kdebug("override_creds() = %p{%d,%d}", old,
  589. atomic_read(&old->usage),
  590. read_cred_subscribers(old));
  591. return old;
  592. }
  593. EXPORT_SYMBOL(override_creds);
  594. /**
  595. * revert_creds - Revert a temporary subjective credentials override
  596. * @old: The credentials to be restored
  597. *
  598. * Revert a temporary set of override subjective credentials to an old set,
  599. * discarding the override set.
  600. */
  601. void revert_creds(const struct cred *old)
  602. {
  603. const struct cred *override = current->cred;
  604. kdebug("revert_creds(%p{%d,%d})", old,
  605. atomic_read(&old->usage),
  606. read_cred_subscribers(old));
  607. validate_creds(old);
  608. validate_creds(override);
  609. alter_cred_subscribers(old, 1);
  610. rcu_assign_pointer(current->cred, old);
  611. trace_android_rvh_revert_creds(current, old);
  612. alter_cred_subscribers(override, -1);
  613. #ifdef CONFIG_KDP_CRED
  614. if (kdp_enable) {
  615. if (is_kdp_protect_addr((unsigned long)override)){
  616. if(GET_ROCRED_RCU(override)->reflected_cred)
  617. put_cred((struct cred *)GET_ROCRED_RCU(override)->reflected_cred);
  618. put_cred(override);
  619. }
  620. }
  621. #endif
  622. put_cred(override);
  623. }
  624. EXPORT_SYMBOL(revert_creds);
  625. /**
  626. * cred_fscmp - Compare two credentials with respect to filesystem access.
  627. * @a: The first credential
  628. * @b: The second credential
  629. *
  630. * cred_cmp() will return zero if both credentials have the same
  631. * fsuid, fsgid, and supplementary groups. That is, if they will both
  632. * provide the same access to files based on mode/uid/gid.
  633. * If the credentials are different, then either -1 or 1 will
  634. * be returned depending on whether @a comes before or after @b
  635. * respectively in an arbitrary, but stable, ordering of credentials.
  636. *
  637. * Return: -1, 0, or 1 depending on comparison
  638. */
  639. int cred_fscmp(const struct cred *a, const struct cred *b)
  640. {
  641. struct group_info *ga, *gb;
  642. int g;
  643. if (a == b)
  644. return 0;
  645. if (uid_lt(a->fsuid, b->fsuid))
  646. return -1;
  647. if (uid_gt(a->fsuid, b->fsuid))
  648. return 1;
  649. if (gid_lt(a->fsgid, b->fsgid))
  650. return -1;
  651. if (gid_gt(a->fsgid, b->fsgid))
  652. return 1;
  653. ga = a->group_info;
  654. gb = b->group_info;
  655. if (ga == gb)
  656. return 0;
  657. if (ga == NULL)
  658. return -1;
  659. if (gb == NULL)
  660. return 1;
  661. if (ga->ngroups < gb->ngroups)
  662. return -1;
  663. if (ga->ngroups > gb->ngroups)
  664. return 1;
  665. for (g = 0; g < ga->ngroups; g++) {
  666. if (gid_lt(ga->gid[g], gb->gid[g]))
  667. return -1;
  668. if (gid_gt(ga->gid[g], gb->gid[g]))
  669. return 1;
  670. }
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(cred_fscmp);
  674. int set_cred_ucounts(struct cred *new)
  675. {
  676. struct ucounts *new_ucounts, *old_ucounts = new->ucounts;
  677. /*
  678. * This optimization is needed because alloc_ucounts() uses locks
  679. * for table lookups.
  680. */
  681. if (old_ucounts->ns == new->user_ns && uid_eq(old_ucounts->uid, new->uid))
  682. return 0;
  683. if (!(new_ucounts = alloc_ucounts(new->user_ns, new->uid)))
  684. return -EAGAIN;
  685. #ifdef CONFIG_KDP_CRED
  686. set_rocred_ucounts(new, new_ucounts);
  687. #else
  688. new->ucounts = new_ucounts;
  689. #endif
  690. put_ucounts(old_ucounts);
  691. return 0;
  692. }
  693. /*
  694. * initialise the credentials stuff
  695. */
  696. void __init cred_init(void)
  697. {
  698. /* allocate a slab in which we can store credentials */
  699. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred), 0,
  700. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
  701. #ifdef CONFIG_KDP_CRED
  702. kdp_cred_init();
  703. #endif
  704. }
  705. /**
  706. * prepare_kernel_cred - Prepare a set of credentials for a kernel service
  707. * @daemon: A userspace daemon to be used as a reference
  708. *
  709. * Prepare a set of credentials for a kernel service. This can then be used to
  710. * override a task's own credentials so that work can be done on behalf of that
  711. * task that requires a different subjective context.
  712. *
  713. * @daemon is used to provide a base for the security record, but can be NULL.
  714. * If @daemon is supplied, then the security data will be derived from that;
  715. * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
  716. *
  717. * The caller may change these controls afterwards if desired.
  718. *
  719. * Returns the new credentials or NULL if out of memory.
  720. */
  721. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  722. {
  723. const struct cred *old;
  724. struct cred *new;
  725. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  726. if (!new)
  727. return NULL;
  728. kdebug("prepare_kernel_cred() alloc %p", new);
  729. if (daemon)
  730. old = get_task_cred(daemon);
  731. else
  732. old = get_cred(&init_cred);
  733. validate_creds(old);
  734. *new = *old;
  735. new->non_rcu = 0;
  736. atomic_set(&new->usage, 1);
  737. set_cred_subscribers(new, 0);
  738. get_uid(new->user);
  739. get_user_ns(new->user_ns);
  740. get_group_info(new->group_info);
  741. #ifdef CONFIG_KEYS
  742. new->session_keyring = NULL;
  743. new->process_keyring = NULL;
  744. new->thread_keyring = NULL;
  745. new->request_key_auth = NULL;
  746. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  747. #endif
  748. #ifdef CONFIG_SECURITY
  749. new->security = NULL;
  750. #endif
  751. new->ucounts = get_ucounts(new->ucounts);
  752. if (!new->ucounts)
  753. goto error;
  754. if (security_prepare_creds(new, old, GFP_KERNEL_ACCOUNT) < 0)
  755. goto error;
  756. put_cred(old);
  757. validate_creds(new);
  758. return new;
  759. error:
  760. put_cred(new);
  761. put_cred(old);
  762. return NULL;
  763. }
  764. EXPORT_SYMBOL(prepare_kernel_cred);
  765. /**
  766. * set_security_override - Set the security ID in a set of credentials
  767. * @new: The credentials to alter
  768. * @secid: The LSM security ID to set
  769. *
  770. * Set the LSM security ID in a set of credentials so that the subjective
  771. * security is overridden when an alternative set of credentials is used.
  772. */
  773. int set_security_override(struct cred *new, u32 secid)
  774. {
  775. return security_kernel_act_as(new, secid);
  776. }
  777. EXPORT_SYMBOL(set_security_override);
  778. /**
  779. * set_security_override_from_ctx - Set the security ID in a set of credentials
  780. * @new: The credentials to alter
  781. * @secctx: The LSM security context to generate the security ID from.
  782. *
  783. * Set the LSM security ID in a set of credentials so that the subjective
  784. * security is overridden when an alternative set of credentials is used. The
  785. * security ID is specified in string form as a security context to be
  786. * interpreted by the LSM.
  787. */
  788. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  789. {
  790. u32 secid;
  791. int ret;
  792. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  793. if (ret < 0)
  794. return ret;
  795. return set_security_override(new, secid);
  796. }
  797. EXPORT_SYMBOL(set_security_override_from_ctx);
  798. /**
  799. * set_create_files_as - Set the LSM file create context in a set of credentials
  800. * @new: The credentials to alter
  801. * @inode: The inode to take the context from
  802. *
  803. * Change the LSM file creation context in a set of credentials to be the same
  804. * as the object context of the specified inode, so that the new inodes have
  805. * the same MAC context as that inode.
  806. */
  807. int set_create_files_as(struct cred *new, struct inode *inode)
  808. {
  809. if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
  810. return -EINVAL;
  811. new->fsuid = inode->i_uid;
  812. new->fsgid = inode->i_gid;
  813. return security_kernel_create_files_as(new, inode);
  814. }
  815. EXPORT_SYMBOL(set_create_files_as);
  816. #ifdef CONFIG_DEBUG_CREDENTIALS
  817. bool creds_are_invalid(const struct cred *cred)
  818. {
  819. if (cred->magic != CRED_MAGIC)
  820. return true;
  821. return false;
  822. }
  823. EXPORT_SYMBOL(creds_are_invalid);
  824. /*
  825. * dump invalid credentials
  826. */
  827. static void dump_invalid_creds(const struct cred *cred, const char *label,
  828. const struct task_struct *tsk)
  829. {
  830. printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
  831. label, cred,
  832. cred == &init_cred ? "[init]" : "",
  833. cred == tsk->real_cred ? "[real]" : "",
  834. cred == tsk->cred ? "[eff]" : "");
  835. printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
  836. cred->magic, cred->put_addr);
  837. printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
  838. atomic_read(&cred->usage),
  839. read_cred_subscribers(cred));
  840. printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
  841. from_kuid_munged(&init_user_ns, cred->uid),
  842. from_kuid_munged(&init_user_ns, cred->euid),
  843. from_kuid_munged(&init_user_ns, cred->suid),
  844. from_kuid_munged(&init_user_ns, cred->fsuid));
  845. printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
  846. from_kgid_munged(&init_user_ns, cred->gid),
  847. from_kgid_munged(&init_user_ns, cred->egid),
  848. from_kgid_munged(&init_user_ns, cred->sgid),
  849. from_kgid_munged(&init_user_ns, cred->fsgid));
  850. #ifdef CONFIG_SECURITY
  851. printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
  852. if ((unsigned long) cred->security >= PAGE_SIZE &&
  853. (((unsigned long) cred->security & 0xffffff00) !=
  854. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
  855. printk(KERN_ERR "CRED: ->security {%x, %x}\n",
  856. ((u32*)cred->security)[0],
  857. ((u32*)cred->security)[1]);
  858. #endif
  859. }
  860. /*
  861. * report use of invalid credentials
  862. */
  863. void __noreturn __invalid_creds(const struct cred *cred, const char *file, unsigned line)
  864. {
  865. printk(KERN_ERR "CRED: Invalid credentials\n");
  866. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  867. dump_invalid_creds(cred, "Specified", current);
  868. BUG();
  869. }
  870. EXPORT_SYMBOL(__invalid_creds);
  871. /*
  872. * check the credentials on a process
  873. */
  874. void __validate_process_creds(struct task_struct *tsk,
  875. const char *file, unsigned line)
  876. {
  877. if (tsk->cred == tsk->real_cred) {
  878. if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
  879. creds_are_invalid(tsk->cred)))
  880. goto invalid_creds;
  881. } else {
  882. if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
  883. read_cred_subscribers(tsk->cred) < 1 ||
  884. creds_are_invalid(tsk->real_cred) ||
  885. creds_are_invalid(tsk->cred)))
  886. goto invalid_creds;
  887. }
  888. return;
  889. invalid_creds:
  890. printk(KERN_ERR "CRED: Invalid process credentials\n");
  891. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  892. dump_invalid_creds(tsk->real_cred, "Real", tsk);
  893. if (tsk->cred != tsk->real_cred)
  894. dump_invalid_creds(tsk->cred, "Effective", tsk);
  895. else
  896. printk(KERN_ERR "CRED: Effective creds == Real creds\n");
  897. BUG();
  898. }
  899. EXPORT_SYMBOL(__validate_process_creds);
  900. /*
  901. * check creds for do_exit()
  902. */
  903. void validate_creds_for_do_exit(struct task_struct *tsk)
  904. {
  905. kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
  906. tsk->real_cred, tsk->cred,
  907. atomic_read(&tsk->cred->usage),
  908. read_cred_subscribers(tsk->cred));
  909. __validate_process_creds(tsk, __FILE__, __LINE__);
  910. }
  911. #endif /* CONFIG_DEBUG_CREDENTIALS */