namespace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author: Andrei Vagin <[email protected]>
  4. * Author: Dmitry Safonov <[email protected]>
  5. */
  6. #include <linux/time_namespace.h>
  7. #include <linux/user_namespace.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/sched/task.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/proc_ns.h>
  13. #include <linux/export.h>
  14. #include <linux/time.h>
  15. #include <linux/slab.h>
  16. #include <linux/cred.h>
  17. #include <linux/err.h>
  18. #include <linux/mm.h>
  19. #include <vdso/datapage.h>
  20. ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
  21. struct timens_offsets *ns_offsets)
  22. {
  23. ktime_t offset;
  24. switch (clockid) {
  25. case CLOCK_MONOTONIC:
  26. offset = timespec64_to_ktime(ns_offsets->monotonic);
  27. break;
  28. case CLOCK_BOOTTIME:
  29. case CLOCK_BOOTTIME_ALARM:
  30. offset = timespec64_to_ktime(ns_offsets->boottime);
  31. break;
  32. default:
  33. return tim;
  34. }
  35. /*
  36. * Check that @tim value is in [offset, KTIME_MAX + offset]
  37. * and subtract offset.
  38. */
  39. if (tim < offset) {
  40. /*
  41. * User can specify @tim *absolute* value - if it's lesser than
  42. * the time namespace's offset - it's already expired.
  43. */
  44. tim = 0;
  45. } else {
  46. tim = ktime_sub(tim, offset);
  47. if (unlikely(tim > KTIME_MAX))
  48. tim = KTIME_MAX;
  49. }
  50. return tim;
  51. }
  52. static struct ucounts *inc_time_namespaces(struct user_namespace *ns)
  53. {
  54. return inc_ucount(ns, current_euid(), UCOUNT_TIME_NAMESPACES);
  55. }
  56. static void dec_time_namespaces(struct ucounts *ucounts)
  57. {
  58. dec_ucount(ucounts, UCOUNT_TIME_NAMESPACES);
  59. }
  60. /**
  61. * clone_time_ns - Clone a time namespace
  62. * @user_ns: User namespace which owns a new namespace.
  63. * @old_ns: Namespace to clone
  64. *
  65. * Clone @old_ns and set the clone refcount to 1
  66. *
  67. * Return: The new namespace or ERR_PTR.
  68. */
  69. static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
  70. struct time_namespace *old_ns)
  71. {
  72. struct time_namespace *ns;
  73. struct ucounts *ucounts;
  74. int err;
  75. err = -ENOSPC;
  76. ucounts = inc_time_namespaces(user_ns);
  77. if (!ucounts)
  78. goto fail;
  79. err = -ENOMEM;
  80. ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
  81. if (!ns)
  82. goto fail_dec;
  83. refcount_set(&ns->ns.count, 1);
  84. ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  85. if (!ns->vvar_page)
  86. goto fail_free;
  87. err = ns_alloc_inum(&ns->ns);
  88. if (err)
  89. goto fail_free_page;
  90. ns->ucounts = ucounts;
  91. ns->ns.ops = &timens_operations;
  92. ns->user_ns = get_user_ns(user_ns);
  93. ns->offsets = old_ns->offsets;
  94. ns->frozen_offsets = false;
  95. return ns;
  96. fail_free_page:
  97. __free_page(ns->vvar_page);
  98. fail_free:
  99. kfree(ns);
  100. fail_dec:
  101. dec_time_namespaces(ucounts);
  102. fail:
  103. return ERR_PTR(err);
  104. }
  105. /**
  106. * copy_time_ns - Create timens_for_children from @old_ns
  107. * @flags: Cloning flags
  108. * @user_ns: User namespace which owns a new namespace.
  109. * @old_ns: Namespace to clone
  110. *
  111. * If CLONE_NEWTIME specified in @flags, creates a new timens_for_children;
  112. * adds a refcounter to @old_ns otherwise.
  113. *
  114. * Return: timens_for_children namespace or ERR_PTR.
  115. */
  116. struct time_namespace *copy_time_ns(unsigned long flags,
  117. struct user_namespace *user_ns, struct time_namespace *old_ns)
  118. {
  119. if (!(flags & CLONE_NEWTIME))
  120. return get_time_ns(old_ns);
  121. return clone_time_ns(user_ns, old_ns);
  122. }
  123. static struct timens_offset offset_from_ts(struct timespec64 off)
  124. {
  125. struct timens_offset ret;
  126. ret.sec = off.tv_sec;
  127. ret.nsec = off.tv_nsec;
  128. return ret;
  129. }
  130. /*
  131. * A time namespace VVAR page has the same layout as the VVAR page which
  132. * contains the system wide VDSO data.
  133. *
  134. * For a normal task the VVAR pages are installed in the normal ordering:
  135. * VVAR
  136. * PVCLOCK
  137. * HVCLOCK
  138. * TIMENS <- Not really required
  139. *
  140. * Now for a timens task the pages are installed in the following order:
  141. * TIMENS
  142. * PVCLOCK
  143. * HVCLOCK
  144. * VVAR
  145. *
  146. * The check for vdso_data->clock_mode is in the unlikely path of
  147. * the seq begin magic. So for the non-timens case most of the time
  148. * 'seq' is even, so the branch is not taken.
  149. *
  150. * If 'seq' is odd, i.e. a concurrent update is in progress, the extra check
  151. * for vdso_data->clock_mode is a non-issue. The task is spin waiting for the
  152. * update to finish and for 'seq' to become even anyway.
  153. *
  154. * Timens page has vdso_data->clock_mode set to VDSO_CLOCKMODE_TIMENS which
  155. * enforces the time namespace handling path.
  156. */
  157. static void timens_setup_vdso_data(struct vdso_data *vdata,
  158. struct time_namespace *ns)
  159. {
  160. struct timens_offset *offset = vdata->offset;
  161. struct timens_offset monotonic = offset_from_ts(ns->offsets.monotonic);
  162. struct timens_offset boottime = offset_from_ts(ns->offsets.boottime);
  163. vdata->seq = 1;
  164. vdata->clock_mode = VDSO_CLOCKMODE_TIMENS;
  165. offset[CLOCK_MONOTONIC] = monotonic;
  166. offset[CLOCK_MONOTONIC_RAW] = monotonic;
  167. offset[CLOCK_MONOTONIC_COARSE] = monotonic;
  168. offset[CLOCK_BOOTTIME] = boottime;
  169. offset[CLOCK_BOOTTIME_ALARM] = boottime;
  170. }
  171. /*
  172. * Protects possibly multiple offsets writers racing each other
  173. * and tasks entering the namespace.
  174. */
  175. static DEFINE_MUTEX(offset_lock);
  176. static void timens_set_vvar_page(struct task_struct *task,
  177. struct time_namespace *ns)
  178. {
  179. struct vdso_data *vdata;
  180. unsigned int i;
  181. if (ns == &init_time_ns)
  182. return;
  183. /* Fast-path, taken by every task in namespace except the first. */
  184. if (likely(ns->frozen_offsets))
  185. return;
  186. mutex_lock(&offset_lock);
  187. /* Nothing to-do: vvar_page has been already initialized. */
  188. if (ns->frozen_offsets)
  189. goto out;
  190. ns->frozen_offsets = true;
  191. vdata = arch_get_vdso_data(page_address(ns->vvar_page));
  192. for (i = 0; i < CS_BASES; i++)
  193. timens_setup_vdso_data(&vdata[i], ns);
  194. out:
  195. mutex_unlock(&offset_lock);
  196. }
  197. void free_time_ns(struct time_namespace *ns)
  198. {
  199. dec_time_namespaces(ns->ucounts);
  200. put_user_ns(ns->user_ns);
  201. ns_free_inum(&ns->ns);
  202. __free_page(ns->vvar_page);
  203. kfree(ns);
  204. }
  205. static struct time_namespace *to_time_ns(struct ns_common *ns)
  206. {
  207. return container_of(ns, struct time_namespace, ns);
  208. }
  209. static struct ns_common *timens_get(struct task_struct *task)
  210. {
  211. struct time_namespace *ns = NULL;
  212. struct nsproxy *nsproxy;
  213. task_lock(task);
  214. nsproxy = task->nsproxy;
  215. if (nsproxy) {
  216. ns = nsproxy->time_ns;
  217. get_time_ns(ns);
  218. }
  219. task_unlock(task);
  220. return ns ? &ns->ns : NULL;
  221. }
  222. static struct ns_common *timens_for_children_get(struct task_struct *task)
  223. {
  224. struct time_namespace *ns = NULL;
  225. struct nsproxy *nsproxy;
  226. task_lock(task);
  227. nsproxy = task->nsproxy;
  228. if (nsproxy) {
  229. ns = nsproxy->time_ns_for_children;
  230. get_time_ns(ns);
  231. }
  232. task_unlock(task);
  233. return ns ? &ns->ns : NULL;
  234. }
  235. static void timens_put(struct ns_common *ns)
  236. {
  237. put_time_ns(to_time_ns(ns));
  238. }
  239. void timens_commit(struct task_struct *tsk, struct time_namespace *ns)
  240. {
  241. timens_set_vvar_page(tsk, ns);
  242. vdso_join_timens(tsk, ns);
  243. }
  244. static int timens_install(struct nsset *nsset, struct ns_common *new)
  245. {
  246. struct nsproxy *nsproxy = nsset->nsproxy;
  247. struct time_namespace *ns = to_time_ns(new);
  248. if (!current_is_single_threaded())
  249. return -EUSERS;
  250. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  251. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  252. return -EPERM;
  253. get_time_ns(ns);
  254. put_time_ns(nsproxy->time_ns);
  255. nsproxy->time_ns = ns;
  256. get_time_ns(ns);
  257. put_time_ns(nsproxy->time_ns_for_children);
  258. nsproxy->time_ns_for_children = ns;
  259. return 0;
  260. }
  261. void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk)
  262. {
  263. struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;
  264. struct time_namespace *ns = to_time_ns(nsc);
  265. /* create_new_namespaces() already incremented the ref counter */
  266. if (nsproxy->time_ns == nsproxy->time_ns_for_children)
  267. return;
  268. get_time_ns(ns);
  269. put_time_ns(nsproxy->time_ns);
  270. nsproxy->time_ns = ns;
  271. timens_commit(tsk, ns);
  272. }
  273. static struct user_namespace *timens_owner(struct ns_common *ns)
  274. {
  275. return to_time_ns(ns)->user_ns;
  276. }
  277. static void show_offset(struct seq_file *m, int clockid, struct timespec64 *ts)
  278. {
  279. char *clock;
  280. switch (clockid) {
  281. case CLOCK_BOOTTIME:
  282. clock = "boottime";
  283. break;
  284. case CLOCK_MONOTONIC:
  285. clock = "monotonic";
  286. break;
  287. default:
  288. clock = "unknown";
  289. break;
  290. }
  291. seq_printf(m, "%-10s %10lld %9ld\n", clock, ts->tv_sec, ts->tv_nsec);
  292. }
  293. void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
  294. {
  295. struct ns_common *ns;
  296. struct time_namespace *time_ns;
  297. ns = timens_for_children_get(p);
  298. if (!ns)
  299. return;
  300. time_ns = to_time_ns(ns);
  301. show_offset(m, CLOCK_MONOTONIC, &time_ns->offsets.monotonic);
  302. show_offset(m, CLOCK_BOOTTIME, &time_ns->offsets.boottime);
  303. put_time_ns(time_ns);
  304. }
  305. int proc_timens_set_offset(struct file *file, struct task_struct *p,
  306. struct proc_timens_offset *offsets, int noffsets)
  307. {
  308. struct ns_common *ns;
  309. struct time_namespace *time_ns;
  310. struct timespec64 tp;
  311. int i, err;
  312. ns = timens_for_children_get(p);
  313. if (!ns)
  314. return -ESRCH;
  315. time_ns = to_time_ns(ns);
  316. if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME)) {
  317. put_time_ns(time_ns);
  318. return -EPERM;
  319. }
  320. for (i = 0; i < noffsets; i++) {
  321. struct proc_timens_offset *off = &offsets[i];
  322. switch (off->clockid) {
  323. case CLOCK_MONOTONIC:
  324. ktime_get_ts64(&tp);
  325. break;
  326. case CLOCK_BOOTTIME:
  327. ktime_get_boottime_ts64(&tp);
  328. break;
  329. default:
  330. err = -EINVAL;
  331. goto out;
  332. }
  333. err = -ERANGE;
  334. if (off->val.tv_sec > KTIME_SEC_MAX ||
  335. off->val.tv_sec < -KTIME_SEC_MAX)
  336. goto out;
  337. tp = timespec64_add(tp, off->val);
  338. /*
  339. * KTIME_SEC_MAX is divided by 2 to be sure that KTIME_MAX is
  340. * still unreachable.
  341. */
  342. if (tp.tv_sec < 0 || tp.tv_sec > KTIME_SEC_MAX / 2)
  343. goto out;
  344. }
  345. mutex_lock(&offset_lock);
  346. if (time_ns->frozen_offsets) {
  347. err = -EACCES;
  348. goto out_unlock;
  349. }
  350. err = 0;
  351. /* Don't report errors after this line */
  352. for (i = 0; i < noffsets; i++) {
  353. struct proc_timens_offset *off = &offsets[i];
  354. struct timespec64 *offset = NULL;
  355. switch (off->clockid) {
  356. case CLOCK_MONOTONIC:
  357. offset = &time_ns->offsets.monotonic;
  358. break;
  359. case CLOCK_BOOTTIME:
  360. offset = &time_ns->offsets.boottime;
  361. break;
  362. }
  363. *offset = off->val;
  364. }
  365. out_unlock:
  366. mutex_unlock(&offset_lock);
  367. out:
  368. put_time_ns(time_ns);
  369. return err;
  370. }
  371. const struct proc_ns_operations timens_operations = {
  372. .name = "time",
  373. .type = CLONE_NEWTIME,
  374. .get = timens_get,
  375. .put = timens_put,
  376. .install = timens_install,
  377. .owner = timens_owner,
  378. };
  379. const struct proc_ns_operations timens_for_children_operations = {
  380. .name = "time_for_children",
  381. .real_ns_name = "time",
  382. .type = CLONE_NEWTIME,
  383. .get = timens_for_children_get,
  384. .put = timens_put,
  385. .install = timens_install,
  386. .owner = timens_owner,
  387. };
  388. struct time_namespace init_time_ns = {
  389. .ns.count = REFCOUNT_INIT(3),
  390. .user_ns = &init_user_ns,
  391. .ns.inum = PROC_TIME_INIT_INO,
  392. .ns.ops = &timens_operations,
  393. .frozen_offsets = true,
  394. };