Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. From b0695a4ca1122066e949207298c100da99bfd79c Mon Sep 17 00:00:00 2001
  2. From: Nikolay Marchuk <marchuk.nikolay.a@gmail.com>
  3. Date: Mon, 27 Mar 2017 12:02:14 +0700
  4. Subject: [PATCH] Implement parser for NS_* ioctl commands
  5.  
  6. ---
  7. Makefile.am | 2 +
  8. configure.ac | 1 +
  9. defs.h | 1 +
  10. ioctl.c | 2 +
  11. nsfs.c | 68 ++++++++++++++++++++++++
  12. nsfs.h | 25 +++++++++
  13. tests/.gitignore | 1 +
  14. tests/Makefile.am | 2 +
  15. tests/ioctl_nsfs.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++
  16. tests/ioctl_nsfs.test | 13 +++++
  17. 10 files changed, 254 insertions(+)
  18. create mode 100644 nsfs.c
  19. create mode 100644 nsfs.h
  20. create mode 100644 tests/ioctl_nsfs.c
  21. create mode 100755 tests/ioctl_nsfs.test
  22.  
  23. diff --git a/Makefile.am b/Makefile.am
  24. index 8af709b..24e94ed 100644
  25. --- a/Makefile.am
  26. +++ b/Makefile.am
  27. @@ -172,6 +172,8 @@ strace_SOURCES = \
  28. net.c \
  29. netlink.c \
  30. nsig.h \
  31. + nsfs.h \
  32. + nsfs.c \
  33. numa.c \
  34. oldstat.c \
  35. open.c \
  36. diff --git a/configure.ac b/configure.ac
  37. index 9e5087b..dc49fdc 100644
  38. --- a/configure.ac
  39. +++ b/configure.ac
  40. @@ -366,6 +366,7 @@ AC_CHECK_HEADERS(m4_normalize([
  41. linux/ipc.h
  42. linux/mmtimer.h
  43. linux/msg.h
  44. + linux/nsfs.h
  45. linux/perf_event.h
  46. linux/quota.h
  47. linux/seccomp.h
  48. diff --git a/defs.h b/defs.h
  49. index 793971e..0f3ec14 100644
  50. --- a/defs.h
  51. +++ b/defs.h
  52. @@ -640,6 +640,7 @@ name ## _ioctl(struct tcb *, unsigned int request, kernel_ulong_t arg)
  53. DECL_IOCTL(dm);
  54. DECL_IOCTL(file);
  55. DECL_IOCTL(fs_x);
  56. +DECL_IOCTL(nsfs);
  57. DECL_IOCTL(ptp);
  58. DECL_IOCTL(scsi);
  59. DECL_IOCTL(term);
  60. diff --git a/ioctl.c b/ioctl.c
  61. index aa1880f..4511e0b 100644
  62. --- a/ioctl.c
  63. +++ b/ioctl.c
  64. @@ -280,6 +280,8 @@ ioctl_decode(struct tcb *tcp)
  65. case 0x94:
  66. return btrfs_ioctl(tcp, code, arg);
  67. #endif
  68. + case 0xb7:
  69. + return nsfs_ioctl(tcp, code, arg);
  70. #ifdef HAVE_LINUX_DM_IOCTL_H
  71. case 0xfd:
  72. return dm_ioctl(tcp, code, arg);
  73. diff --git a/nsfs.c b/nsfs.c
  74. new file mode 100644
  75. index 0000000..28abcb8
  76. --- /dev/null
  77. +++ b/nsfs.c
  78. @@ -0,0 +1,68 @@
  79. +/*
  80. + * Support for decoding of NS_* ioctl commands.
  81. + *
  82. + * Copyright (c) 2017 Nikolay Marchuk <marchuk.nikolay.a@gmail.com>
  83. + * All rights reserved.
  84. + *
  85. + * Redistribution and use in source and binary forms, with or without
  86. + * modification, are permitted provided that the following conditions
  87. + * are met:
  88. + * 1. Redistributions of source code must retain the above copyright
  89. + * notice, this list of conditions and the following disclaimer.
  90. + * 2. Redistributions in binary form must reproduce the above copyright
  91. + * notice, this list of conditions and the following disclaimer in the
  92. + * documentation and/or other materials provided with the distribution.
  93. + * 3. The name of the author may not be used to endorse or promote products
  94. + * derived from this software without specific prior written permission.
  95. + *
  96. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  97. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  98. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  99. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  100. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  101. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  102. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  103. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  104. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  105. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  106. + */
  107. +
  108. +#include "defs.h"
  109. +
  110. +#include <linux/ioctl.h>
  111. +#include "nsfs.h"
  112. +#include "xlat/setns_types.h"
  113. +
  114. +int
  115. +nsfs_ioctl(struct tcb *tcp, unsigned int code, kernel_ulong_t arg)
  116. +{
  117. + const char *outstr;
  118. + uid_t uid;
  119. + switch (code) {
  120. + case NS_GET_USERNS:
  121. + case NS_GET_PARENT:
  122. + return 1 + RVAL_FD + RVAL_DECODED;
  123. + case NS_GET_NSTYPE:
  124. + if (entering(tcp))
  125. + return 0;
  126. + if (!syserror(tcp)) {
  127. + outstr = xlookup(setns_types, tcp->u_rval);
  128. + if (outstr) {
  129. + tcp->auxstr = outstr;
  130. + return 1 + RVAL_STR;
  131. + }
  132. + }
  133. + return 1;
  134. + case NS_GET_OWNER_UID:
  135. + if (entering(tcp))
  136. + return 0;
  137. + tprints(", ");
  138. + if (!umove_or_printaddr(tcp, arg, &uid)) {
  139. + printuid("[", uid);
  140. + tprints("]");
  141. + }
  142. + return 1;
  143. + default:
  144. + return 0;
  145. + }
  146. +}
  147. diff --git a/nsfs.h b/nsfs.h
  148. new file mode 100644
  149. index 0000000..cda1758
  150. --- /dev/null
  151. +++ b/nsfs.h
  152. @@ -0,0 +1,25 @@
  153. +#ifndef STRACE_NSFS_H
  154. +#define STRACE_NSFS_H
  155. +
  156. +# ifdef HAVE_LINUX_NSFS_H
  157. +# include <linux/nsfs.h>
  158. +# else
  159. +# ifndef NSIO
  160. +# define NSIO 0xb7
  161. +# endif
  162. +# ifndef NS_GET_USERNS
  163. +# define NS_GET_USERNS _IO(NSIO, 0x1)
  164. +# endif
  165. +# ifndef NS_GET_PARENT
  166. +# define NS_GET_PARENT _IO(NSIO, 0x2)
  167. +# endif
  168. +# endif
  169. +
  170. +# ifndef NS_GET_NSTYPE
  171. +# define NS_GET_NSTYPE _IO(NSIO, 0x3)
  172. +# endif
  173. +# ifndef NS_GET_OWNER_UID
  174. +# define NS_GET_OWNER_UID _IO(NSIO, 0x4)
  175. +# endif
  176. +
  177. +#endif /* !STRACE_NSFS_H */
  178. diff --git a/tests/.gitignore b/tests/.gitignore
  179. index 477529d..1998097 100644
  180. --- a/tests/.gitignore
  181. +++ b/tests/.gitignore
  182. @@ -122,6 +122,7 @@ ioctl_loop
  183. ioctl_loop-nv
  184. ioctl_loop-v
  185. ioctl_mtd
  186. +ioctl_nsfs
  187. ioctl_rtc
  188. ioctl_rtc-v
  189. ioctl_scsi
  190. diff --git a/tests/Makefile.am b/tests/Makefile.am
  191. index c6c28cc..44b0463 100644
  192. --- a/tests/Makefile.am
  193. +++ b/tests/Makefile.am
  194. @@ -185,6 +185,7 @@ check_PROGRAMS = \
  195. ioctl_loop-nv \
  196. ioctl_loop-v \
  197. ioctl_mtd \
  198. + ioctl_nsfs \
  199. ioctl_rtc \
  200. ioctl_rtc-v \
  201. ioctl_scsi \
  202. @@ -595,6 +596,7 @@ DECODER_TESTS = \
  203. ioctl_loop-v.test \
  204. ioctl_loop.test \
  205. ioctl_mtd.test \
  206. + ioctl_nsfs.test \
  207. ioctl_rtc-v.test \
  208. ioctl_rtc.test \
  209. ioctl_scsi.test \
  210. diff --git a/tests/ioctl_nsfs.c b/tests/ioctl_nsfs.c
  211. new file mode 100644
  212. index 0000000..9d41adf
  213. --- /dev/null
  214. +++ b/tests/ioctl_nsfs.c
  215. @@ -0,0 +1,139 @@
  216. +/*
  217. + * Check decoding of NS_* commands of ioctl syscall.
  218. + *
  219. + * Copyright (c) 2017 Nikolay Marchuk <marchuk.nikolay.a@gmail.com>
  220. + * All rights reserved.
  221. + *
  222. + * Redistribution and use in source and binary forms, with or without
  223. + * modification, are permitted provided that the following conditions
  224. + * are met:
  225. + * 1. Redistributions of source code must retain the above copyright
  226. + * notice, this list of conditions and the following disclaimer.
  227. + * 2. Redistributions in binary form must reproduce the above copyright
  228. + * notice, this list of conditions and the following disclaimer in the
  229. + * documentation and/or other materials provided with the distribution.
  230. + * 3. The name of the author may not be used to endorse or promote products
  231. + * derived from this software without specific prior written permission.
  232. + *
  233. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  234. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  235. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  236. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  237. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  238. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  239. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  240. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  241. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  242. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  243. + */
  244. +
  245. +#include "tests.h"
  246. +
  247. +#include <fcntl.h>
  248. +#include <linux/ioctl.h>
  249. +#include <sched.h>
  250. +#include <stdio.h>
  251. +#include <stdlib.h>
  252. +#include <sys/ioctl.h>
  253. +#include <unistd.h>
  254. +#include "nsfs.h"
  255. +
  256. +
  257. +static void
  258. +test_no_namespace(void)
  259. +{
  260. + ioctl(-1, NS_GET_USERNS);
  261. + printf("ioctl(-1, NS_GET_USERNS) = -1 EBADF (%m)\n");
  262. + ioctl(-1, NS_GET_PARENT);
  263. + printf("ioctl(-1, NS_GET_PARENT) = -1 EBADF (%m)\n");
  264. + ioctl(-1, NS_GET_NSTYPE);
  265. + printf("ioctl(-1, NS_GET_NSTYPE) = -1 EBADF (%m)\n");
  266. + ioctl(-1, NS_GET_OWNER_UID, NULL);
  267. + printf("ioctl(-1, NS_GET_OWNER_UID, NULL) = -1 EBADF (%m)\n");
  268. +}
  269. +
  270. +static void
  271. +test_clone(pid_t pid)
  272. +{
  273. + int ns_fd, userns_fd, parent_ns_fd, nstype, rc;
  274. + const char *errstr;
  275. + char path[22];
  276. + TAIL_ALLOC_OBJECT_CONST_PTR(uid_t, uid);
  277. +
  278. + snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
  279. + ns_fd = open(path, O_RDONLY);
  280. + if (ns_fd == -1)
  281. + perror_msg_and_skip("open: %s", path);
  282. +
  283. + userns_fd = ioctl(ns_fd, NS_GET_USERNS);
  284. + printf("ioctl(%d, NS_GET_USERNS) = %s\n", ns_fd, sprintrc(userns_fd));
  285. +
  286. + parent_ns_fd = ioctl(userns_fd, NS_GET_PARENT);
  287. + printf("ioctl(%d, NS_GET_PARENT) = %s\n", userns_fd,
  288. + sprintrc(parent_ns_fd));
  289. +
  290. + nstype = ioctl(userns_fd, NS_GET_NSTYPE);
  291. + errstr = sprintrc(nstype);
  292. + if (nstype == -1) {
  293. + printf("ioctl(%d, NS_GET_NSTYPE) = %s\n", userns_fd, errstr);
  294. + } else {
  295. + printf("ioctl(%d, NS_GET_NSTYPE) = %d (CLONE_NEWUSER)\n", userns_fd,
  296. + nstype);
  297. + }
  298. +
  299. + rc = ioctl(userns_fd, NS_GET_OWNER_UID, &uid);
  300. + errstr = sprintrc(rc);
  301. + if (rc == -1) {
  302. + printf("ioctl(%d, NS_GET_OWNER_UID, %p) = %s\n", userns_fd, &uid,
  303. + errstr);
  304. + } else {
  305. + printf("ioctl(%d, NS_GET_OWNER_UID, [", userns_fd);
  306. + if ((uid_t) -1U == (uid_t) uid)
  307. + printf("-1]) = %s\n", errstr);
  308. + else
  309. + printf("%u]) = %s\n", uid, errstr);
  310. + }
  311. +}
  312. +
  313. +static int
  314. +child(void *arg)
  315. +{
  316. + char c;
  317. + int *pipefd = (int *)arg;
  318. + while (read(pipefd[1], &c, 1) != 1);
  319. + close(pipefd[1]);
  320. + return 0;
  321. +}
  322. +
  323. +#define STACK_SIZE (1024 * 1024)
  324. +
  325. +static void
  326. +test_user_namespace(void)
  327. +{
  328. + char stack[STACK_SIZE];
  329. + pid_t pid;
  330. + int pipefd[2];
  331. + int rc;
  332. +
  333. + rc = pipe(pipefd);
  334. + if (rc == -1)
  335. + perror_msg_and_skip("pipe");
  336. +
  337. + pid = clone(child, stack + STACK_SIZE, CLONE_NEWUSER | CLONE_UNTRACED,
  338. + pipefd);
  339. + if (pid == -1)
  340. + perror_msg_and_skip("clone");
  341. +
  342. + test_clone(pid);
  343. + write(pipefd[0], "", 1);
  344. + close(pipefd[0]);
  345. +}
  346. +
  347. +int
  348. +main(void)
  349. +{
  350. + test_no_namespace();
  351. + test_user_namespace();
  352. + puts("+++ exited with 0 +++");
  353. + return 0;
  354. +}
  355. diff --git a/tests/ioctl_nsfs.test b/tests/ioctl_nsfs.test
  356. new file mode 100755
  357. index 0000000..8fe8da5
  358. --- /dev/null
  359. +++ b/tests/ioctl_nsfs.test
  360. @@ -0,0 +1,13 @@
  361. +#!/bin/sh
  362. +
  363. +# Check decoding of NS_* ioctls.
  364. +
  365. +. "${srcdir=.}/init.sh"
  366. +
  367. +run_prog > /dev/null
  368. +run_strace -a16 -eioctl $args > "$EXP"
  369. +check_prog grep
  370. +grep -v '^ioctl([012],' < "$LOG" > "$OUT"
  371. +match_diff "$OUT" "$EXP"
  372. +
  373. +rm -f "$EXP" "$OUT"
  374. --
  375. 2.1.4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement