virtfs-proxy-helper.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Helper for QEMU Proxy FS Driver
  3. * Copyright IBM, Corp. 2011
  4. *
  5. * Authors:
  6. * M. Mohan Kumar <mohan@in.ibm.com>
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2. See
  9. * the COPYING file in the top-level directory.
  10. */
  11. #include <sys/resource.h>
  12. #include <getopt.h>
  13. #include <syslog.h>
  14. #include <sys/capability.h>
  15. #include <sys/fsuid.h>
  16. #include <sys/vfs.h>
  17. #include <sys/ioctl.h>
  18. #include <linux/fs.h>
  19. #ifdef CONFIG_LINUX_MAGIC_H
  20. #include <linux/magic.h>
  21. #endif
  22. #include "qemu-common.h"
  23. #include "qemu/sockets.h"
  24. #include "qemu/xattr.h"
  25. #include "virtio-9p-marshal.h"
  26. #include "hw/9pfs/virtio-9p-proxy.h"
  27. #include "fsdev/virtio-9p-marshal.h"
  28. #define PROGNAME "virtfs-proxy-helper"
  29. #ifndef XFS_SUPER_MAGIC
  30. #define XFS_SUPER_MAGIC 0x58465342
  31. #endif
  32. #ifndef EXT2_SUPER_MAGIC
  33. #define EXT2_SUPER_MAGIC 0xEF53
  34. #endif
  35. #ifndef REISERFS_SUPER_MAGIC
  36. #define REISERFS_SUPER_MAGIC 0x52654973
  37. #endif
  38. #ifndef BTRFS_SUPER_MAGIC
  39. #define BTRFS_SUPER_MAGIC 0x9123683E
  40. #endif
  41. static struct option helper_opts[] = {
  42. {"fd", required_argument, NULL, 'f'},
  43. {"path", required_argument, NULL, 'p'},
  44. {"nodaemon", no_argument, NULL, 'n'},
  45. {"socket", required_argument, NULL, 's'},
  46. {"uid", required_argument, NULL, 'u'},
  47. {"gid", required_argument, NULL, 'g'},
  48. };
  49. static bool is_daemon;
  50. static bool get_version; /* IOC getversion IOCTL supported */
  51. static void GCC_FMT_ATTR(2, 3) do_log(int loglevel, const char *format, ...)
  52. {
  53. va_list ap;
  54. va_start(ap, format);
  55. if (is_daemon) {
  56. vsyslog(LOG_CRIT, format, ap);
  57. } else {
  58. vfprintf(stderr, format, ap);
  59. }
  60. va_end(ap);
  61. }
  62. static void do_perror(const char *string)
  63. {
  64. if (is_daemon) {
  65. syslog(LOG_CRIT, "%s:%s", string, strerror(errno));
  66. } else {
  67. fprintf(stderr, "%s:%s\n", string, strerror(errno));
  68. }
  69. }
  70. static int do_cap_set(cap_value_t *cap_value, int size, int reset)
  71. {
  72. cap_t caps;
  73. if (reset) {
  74. /*
  75. * Start with an empty set and set permitted and effective
  76. */
  77. caps = cap_init();
  78. if (caps == NULL) {
  79. do_perror("cap_init");
  80. return -1;
  81. }
  82. if (cap_set_flag(caps, CAP_PERMITTED, size, cap_value, CAP_SET) < 0) {
  83. do_perror("cap_set_flag");
  84. goto error;
  85. }
  86. } else {
  87. caps = cap_get_proc();
  88. if (!caps) {
  89. do_perror("cap_get_proc");
  90. return -1;
  91. }
  92. }
  93. if (cap_set_flag(caps, CAP_EFFECTIVE, size, cap_value, CAP_SET) < 0) {
  94. do_perror("cap_set_flag");
  95. goto error;
  96. }
  97. if (cap_set_proc(caps) < 0) {
  98. do_perror("cap_set_proc");
  99. goto error;
  100. }
  101. cap_free(caps);
  102. return 0;
  103. error:
  104. cap_free(caps);
  105. return -1;
  106. }
  107. static int init_capabilities(void)
  108. {
  109. /* helper needs following capbabilities only */
  110. cap_value_t cap_list[] = {
  111. CAP_CHOWN,
  112. CAP_DAC_OVERRIDE,
  113. CAP_FOWNER,
  114. CAP_FSETID,
  115. CAP_SETGID,
  116. CAP_MKNOD,
  117. CAP_SETUID,
  118. };
  119. return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 1);
  120. }
  121. static int socket_read(int sockfd, void *buff, ssize_t size)
  122. {
  123. ssize_t retval, total = 0;
  124. while (size) {
  125. retval = read(sockfd, buff, size);
  126. if (retval == 0) {
  127. return -EIO;
  128. }
  129. if (retval < 0) {
  130. if (errno == EINTR) {
  131. continue;
  132. }
  133. return -errno;
  134. }
  135. size -= retval;
  136. buff += retval;
  137. total += retval;
  138. }
  139. return total;
  140. }
  141. static int socket_write(int sockfd, void *buff, ssize_t size)
  142. {
  143. ssize_t retval, total = 0;
  144. while (size) {
  145. retval = write(sockfd, buff, size);
  146. if (retval < 0) {
  147. if (errno == EINTR) {
  148. continue;
  149. }
  150. return -errno;
  151. }
  152. size -= retval;
  153. buff += retval;
  154. total += retval;
  155. }
  156. return total;
  157. }
  158. static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header)
  159. {
  160. int retval;
  161. /*
  162. * read the request header.
  163. */
  164. iovec->iov_len = 0;
  165. retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ);
  166. if (retval < 0) {
  167. return retval;
  168. }
  169. iovec->iov_len = PROXY_HDR_SZ;
  170. retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size);
  171. if (retval < 0) {
  172. return retval;
  173. }
  174. /*
  175. * We can't process message.size > PROXY_MAX_IO_SZ.
  176. * Treat it as fatal error
  177. */
  178. if (header->size > PROXY_MAX_IO_SZ) {
  179. return -ENOBUFS;
  180. }
  181. retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size);
  182. if (retval < 0) {
  183. return retval;
  184. }
  185. iovec->iov_len += header->size;
  186. return 0;
  187. }
  188. static int send_fd(int sockfd, int fd)
  189. {
  190. struct msghdr msg;
  191. struct iovec iov;
  192. int retval, data;
  193. struct cmsghdr *cmsg;
  194. union MsgControl msg_control;
  195. iov.iov_base = &data;
  196. iov.iov_len = sizeof(data);
  197. memset(&msg, 0, sizeof(msg));
  198. msg.msg_iov = &iov;
  199. msg.msg_iovlen = 1;
  200. /* No ancillary data on error */
  201. if (fd < 0) {
  202. /* fd is really negative errno if the request failed */
  203. data = fd;
  204. } else {
  205. data = V9FS_FD_VALID;
  206. msg.msg_control = &msg_control;
  207. msg.msg_controllen = sizeof(msg_control);
  208. cmsg = &msg_control.cmsg;
  209. cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
  210. cmsg->cmsg_level = SOL_SOCKET;
  211. cmsg->cmsg_type = SCM_RIGHTS;
  212. memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
  213. }
  214. do {
  215. retval = sendmsg(sockfd, &msg, 0);
  216. } while (retval < 0 && errno == EINTR);
  217. if (fd >= 0) {
  218. close(fd);
  219. }
  220. if (retval < 0) {
  221. return retval;
  222. }
  223. return 0;
  224. }
  225. static int send_status(int sockfd, struct iovec *iovec, int status)
  226. {
  227. ProxyHeader header;
  228. int retval, msg_size;
  229. if (status < 0) {
  230. header.type = T_ERROR;
  231. } else {
  232. header.type = T_SUCCESS;
  233. }
  234. header.size = sizeof(status);
  235. /*
  236. * marshal the return status. We don't check error.
  237. * because we are sure we have enough space for the status
  238. */
  239. msg_size = proxy_marshal(iovec, 0, "ddd", header.type,
  240. header.size, status);
  241. if (msg_size < 0) {
  242. return msg_size;
  243. }
  244. retval = socket_write(sockfd, iovec->iov_base, msg_size);
  245. if (retval < 0) {
  246. return retval;
  247. }
  248. return 0;
  249. }
  250. /*
  251. * from man 7 capabilities, section
  252. * Effect of User ID Changes on Capabilities:
  253. * If the effective user ID is changed from nonzero to 0, then the permitted
  254. * set is copied to the effective set. If the effective user ID is changed
  255. * from 0 to nonzero, then all capabilities are are cleared from the effective
  256. * set.
  257. *
  258. * The setfsuid/setfsgid man pages warn that changing the effective user ID may
  259. * expose the program to unwanted signals, but this is not true anymore: for an
  260. * unprivileged (without CAP_KILL) program to send a signal, the real or
  261. * effective user ID of the sending process must equal the real or saved user
  262. * ID of the target process. Even when dropping privileges, it is enough to
  263. * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
  264. * be exposed to signals. So just use setresuid/setresgid.
  265. */
  266. static int setugid(int uid, int gid, int *suid, int *sgid)
  267. {
  268. int retval;
  269. /*
  270. * We still need DAC_OVERRIDE because we don't change
  271. * supplementary group ids, and hence may be subjected DAC rules
  272. */
  273. cap_value_t cap_list[] = {
  274. CAP_DAC_OVERRIDE,
  275. };
  276. *suid = geteuid();
  277. *sgid = getegid();
  278. if (setresgid(-1, gid, *sgid) == -1) {
  279. retval = -errno;
  280. goto err_out;
  281. }
  282. if (setresuid(-1, uid, *suid) == -1) {
  283. retval = -errno;
  284. goto err_sgid;
  285. }
  286. if (uid != 0 || gid != 0) {
  287. if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) {
  288. retval = -errno;
  289. goto err_suid;
  290. }
  291. }
  292. return 0;
  293. err_suid:
  294. if (setresuid(-1, *suid, *suid) == -1) {
  295. abort();
  296. }
  297. err_sgid:
  298. if (setresgid(-1, *sgid, *sgid) == -1) {
  299. abort();
  300. }
  301. err_out:
  302. return retval;
  303. }
  304. /*
  305. * This is used to reset the ugid back with the saved values
  306. * There is nothing much we can do checking error values here.
  307. */
  308. static void resetugid(int suid, int sgid)
  309. {
  310. if (setresgid(-1, sgid, sgid) == -1) {
  311. abort();
  312. }
  313. if (setresuid(-1, suid, suid) == -1) {
  314. abort();
  315. }
  316. }
  317. /*
  318. * send response in two parts
  319. * 1) ProxyHeader
  320. * 2) Response or error status
  321. * This function should be called with marshaled response
  322. * send_response constructs header part and error part only.
  323. * send response sends {ProxyHeader,Response} if the request was success
  324. * otherwise sends {ProxyHeader,error status}
  325. */
  326. static int send_response(int sock, struct iovec *iovec, int size)
  327. {
  328. int retval;
  329. ProxyHeader header;
  330. /*
  331. * If response size exceeds available iovec->iov_len,
  332. * we return ENOBUFS
  333. */
  334. if (size > PROXY_MAX_IO_SZ) {
  335. size = -ENOBUFS;
  336. }
  337. if (size < 0) {
  338. /*
  339. * In case of error we would not have got the error encoded
  340. * already so encode the error here.
  341. */
  342. header.type = T_ERROR;
  343. header.size = sizeof(size);
  344. proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
  345. } else {
  346. header.type = T_SUCCESS;
  347. header.size = size;
  348. }
  349. proxy_marshal(iovec, 0, "dd", header.type, header.size);
  350. retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
  351. if (retval < 0) {
  352. return retval;
  353. }
  354. return 0;
  355. }
  356. /*
  357. * gets generation number
  358. * returns -errno on failure and sizeof(generation number) on success
  359. */
  360. static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
  361. {
  362. uint64_t version;
  363. int retval = -ENOTTY;
  364. #ifdef FS_IOC_GETVERSION
  365. int fd;
  366. V9fsString path;
  367. #endif
  368. /* no need to issue ioctl */
  369. if (!get_version) {
  370. version = 0;
  371. retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
  372. return retval;
  373. }
  374. #ifdef FS_IOC_GETVERSION
  375. retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
  376. if (retval < 0) {
  377. return retval;
  378. }
  379. fd = open(path.data, O_RDONLY);
  380. if (fd < 0) {
  381. retval = -errno;
  382. goto err_out;
  383. }
  384. if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
  385. retval = -errno;
  386. } else {
  387. retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
  388. }
  389. close(fd);
  390. err_out:
  391. v9fs_string_free(&path);
  392. #endif
  393. return retval;
  394. }
  395. static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
  396. {
  397. int size = 0, offset, retval;
  398. V9fsString path, name, xattr;
  399. v9fs_string_init(&xattr);
  400. v9fs_string_init(&path);
  401. retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
  402. if (retval < 0) {
  403. return retval;
  404. }
  405. offset = PROXY_HDR_SZ + retval;
  406. if (size) {
  407. xattr.data = g_malloc(size);
  408. xattr.size = size;
  409. }
  410. switch (type) {
  411. case T_LGETXATTR:
  412. v9fs_string_init(&name);
  413. retval = proxy_unmarshal(iovec, offset, "s", &name);
  414. if (retval > 0) {
  415. retval = lgetxattr(path.data, name.data, xattr.data, size);
  416. if (retval < 0) {
  417. retval = -errno;
  418. } else {
  419. xattr.size = retval;
  420. }
  421. }
  422. v9fs_string_free(&name);
  423. break;
  424. case T_LLISTXATTR:
  425. retval = llistxattr(path.data, xattr.data, size);
  426. if (retval < 0) {
  427. retval = -errno;
  428. } else {
  429. xattr.size = retval;
  430. }
  431. break;
  432. }
  433. if (retval < 0) {
  434. goto err_out;
  435. }
  436. if (!size) {
  437. proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
  438. retval = sizeof(retval);
  439. } else {
  440. retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
  441. }
  442. err_out:
  443. v9fs_string_free(&xattr);
  444. v9fs_string_free(&path);
  445. return retval;
  446. }
  447. static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
  448. {
  449. memset(pr_stat, 0, sizeof(*pr_stat));
  450. pr_stat->st_dev = stat->st_dev;
  451. pr_stat->st_ino = stat->st_ino;
  452. pr_stat->st_nlink = stat->st_nlink;
  453. pr_stat->st_mode = stat->st_mode;
  454. pr_stat->st_uid = stat->st_uid;
  455. pr_stat->st_gid = stat->st_gid;
  456. pr_stat->st_rdev = stat->st_rdev;
  457. pr_stat->st_size = stat->st_size;
  458. pr_stat->st_blksize = stat->st_blksize;
  459. pr_stat->st_blocks = stat->st_blocks;
  460. pr_stat->st_atim_sec = stat->st_atim.tv_sec;
  461. pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
  462. pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
  463. pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
  464. pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
  465. pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
  466. }
  467. static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
  468. {
  469. memset(pr_stfs, 0, sizeof(*pr_stfs));
  470. pr_stfs->f_type = stfs->f_type;
  471. pr_stfs->f_bsize = stfs->f_bsize;
  472. pr_stfs->f_blocks = stfs->f_blocks;
  473. pr_stfs->f_bfree = stfs->f_bfree;
  474. pr_stfs->f_bavail = stfs->f_bavail;
  475. pr_stfs->f_files = stfs->f_files;
  476. pr_stfs->f_ffree = stfs->f_ffree;
  477. pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
  478. pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
  479. pr_stfs->f_namelen = stfs->f_namelen;
  480. pr_stfs->f_frsize = stfs->f_frsize;
  481. }
  482. /*
  483. * Gets stat/statfs information and packs in out_iovec structure
  484. * on success returns number of bytes packed in out_iovec struture
  485. * otherwise returns -errno
  486. */
  487. static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
  488. {
  489. int retval;
  490. V9fsString path;
  491. ProxyStat pr_stat;
  492. ProxyStatFS pr_stfs;
  493. struct stat st_buf;
  494. struct statfs stfs_buf;
  495. v9fs_string_init(&path);
  496. retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
  497. if (retval < 0) {
  498. return retval;
  499. }
  500. switch (type) {
  501. case T_LSTAT:
  502. retval = lstat(path.data, &st_buf);
  503. if (retval < 0) {
  504. retval = -errno;
  505. } else {
  506. stat_to_prstat(&pr_stat, &st_buf);
  507. retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
  508. "qqqdddqqqqqqqqqq", pr_stat.st_dev,
  509. pr_stat.st_ino, pr_stat.st_nlink,
  510. pr_stat.st_mode, pr_stat.st_uid,
  511. pr_stat.st_gid, pr_stat.st_rdev,
  512. pr_stat.st_size, pr_stat.st_blksize,
  513. pr_stat.st_blocks,
  514. pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
  515. pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
  516. pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
  517. }
  518. break;
  519. case T_STATFS:
  520. retval = statfs(path.data, &stfs_buf);
  521. if (retval < 0) {
  522. retval = -errno;
  523. } else {
  524. statfs_to_prstatfs(&pr_stfs, &stfs_buf);
  525. retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
  526. "qqqqqqqqqqq", pr_stfs.f_type,
  527. pr_stfs.f_bsize, pr_stfs.f_blocks,
  528. pr_stfs.f_bfree, pr_stfs.f_bavail,
  529. pr_stfs.f_files, pr_stfs.f_ffree,
  530. pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
  531. pr_stfs.f_namelen, pr_stfs.f_frsize);
  532. }
  533. break;
  534. }
  535. v9fs_string_free(&path);
  536. return retval;
  537. }
  538. static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
  539. {
  540. char *buffer;
  541. int size, retval;
  542. V9fsString target, path;
  543. v9fs_string_init(&path);
  544. retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
  545. if (retval < 0) {
  546. v9fs_string_free(&path);
  547. return retval;
  548. }
  549. buffer = g_malloc(size);
  550. v9fs_string_init(&target);
  551. retval = readlink(path.data, buffer, size - 1);
  552. if (retval > 0) {
  553. buffer[retval] = '\0';
  554. v9fs_string_sprintf(&target, "%s", buffer);
  555. retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
  556. } else {
  557. retval = -errno;
  558. }
  559. g_free(buffer);
  560. v9fs_string_free(&target);
  561. v9fs_string_free(&path);
  562. return retval;
  563. }
  564. /*
  565. * create other filesystem objects and send 0 on success
  566. * return -errno on error
  567. */
  568. static int do_create_others(int type, struct iovec *iovec)
  569. {
  570. dev_t rdev;
  571. int retval = 0;
  572. int offset = PROXY_HDR_SZ;
  573. V9fsString oldpath, path;
  574. int mode, uid, gid, cur_uid, cur_gid;
  575. v9fs_string_init(&path);
  576. v9fs_string_init(&oldpath);
  577. retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
  578. if (retval < 0) {
  579. return retval;
  580. }
  581. offset += retval;
  582. retval = setugid(uid, gid, &cur_uid, &cur_gid);
  583. if (retval < 0) {
  584. goto unmarshal_err_out;
  585. }
  586. switch (type) {
  587. case T_MKNOD:
  588. retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
  589. if (retval < 0) {
  590. goto err_out;
  591. }
  592. retval = mknod(path.data, mode, rdev);
  593. break;
  594. case T_MKDIR:
  595. retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
  596. if (retval < 0) {
  597. goto err_out;
  598. }
  599. retval = mkdir(path.data, mode);
  600. break;
  601. case T_SYMLINK:
  602. retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
  603. if (retval < 0) {
  604. goto err_out;
  605. }
  606. retval = symlink(oldpath.data, path.data);
  607. break;
  608. }
  609. if (retval < 0) {
  610. retval = -errno;
  611. }
  612. err_out:
  613. resetugid(cur_uid, cur_gid);
  614. unmarshal_err_out:
  615. v9fs_string_free(&path);
  616. v9fs_string_free(&oldpath);
  617. return retval;
  618. }
  619. /*
  620. * create a file and send fd on success
  621. * return -errno on error
  622. */
  623. static int do_create(struct iovec *iovec)
  624. {
  625. int ret;
  626. V9fsString path;
  627. int flags, mode, uid, gid, cur_uid, cur_gid;
  628. v9fs_string_init(&path);
  629. ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
  630. &path, &flags, &mode, &uid, &gid);
  631. if (ret < 0) {
  632. goto unmarshal_err_out;
  633. }
  634. ret = setugid(uid, gid, &cur_uid, &cur_gid);
  635. if (ret < 0) {
  636. goto unmarshal_err_out;
  637. }
  638. ret = open(path.data, flags, mode);
  639. if (ret < 0) {
  640. ret = -errno;
  641. }
  642. resetugid(cur_uid, cur_gid);
  643. unmarshal_err_out:
  644. v9fs_string_free(&path);
  645. return ret;
  646. }
  647. /*
  648. * open a file and send fd on success
  649. * return -errno on error
  650. */
  651. static int do_open(struct iovec *iovec)
  652. {
  653. int flags, ret;
  654. V9fsString path;
  655. v9fs_string_init(&path);
  656. ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
  657. if (ret < 0) {
  658. goto err_out;
  659. }
  660. ret = open(path.data, flags);
  661. if (ret < 0) {
  662. ret = -errno;
  663. }
  664. err_out:
  665. v9fs_string_free(&path);
  666. return ret;
  667. }
  668. /* create unix domain socket and return the descriptor */
  669. static int proxy_socket(const char *path, uid_t uid, gid_t gid)
  670. {
  671. int sock, client;
  672. struct sockaddr_un proxy, qemu;
  673. socklen_t size;
  674. /* requested socket already exists, refuse to start */
  675. if (!access(path, F_OK)) {
  676. do_log(LOG_CRIT, "socket already exists\n");
  677. return -1;
  678. }
  679. sock = socket(AF_UNIX, SOCK_STREAM, 0);
  680. if (sock < 0) {
  681. do_perror("socket");
  682. return -1;
  683. }
  684. /* mask other part of mode bits */
  685. umask(7);
  686. proxy.sun_family = AF_UNIX;
  687. strcpy(proxy.sun_path, path);
  688. if (bind(sock, (struct sockaddr *)&proxy,
  689. sizeof(struct sockaddr_un)) < 0) {
  690. do_perror("bind");
  691. goto error;
  692. }
  693. if (chown(proxy.sun_path, uid, gid) < 0) {
  694. do_perror("chown");
  695. goto error;
  696. }
  697. if (listen(sock, 1) < 0) {
  698. do_perror("listen");
  699. goto error;
  700. }
  701. size = sizeof(qemu);
  702. client = accept(sock, (struct sockaddr *)&qemu, &size);
  703. if (client < 0) {
  704. do_perror("accept");
  705. goto error;
  706. }
  707. close(sock);
  708. return client;
  709. error:
  710. close(sock);
  711. return -1;
  712. }
  713. static void usage(char *prog)
  714. {
  715. fprintf(stderr, "usage: %s\n"
  716. " -p|--path <path> 9p path to export\n"
  717. " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
  718. " {-s|--socket <socketname> socket file used for communication\n"
  719. " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
  720. " access to this socket\n"
  721. " \tNote: -s & -f can not be used together\n"
  722. " [-n|--nodaemon] Run as a normal program\n",
  723. basename(prog));
  724. }
  725. static int process_reply(int sock, int type,
  726. struct iovec *out_iovec, int retval)
  727. {
  728. switch (type) {
  729. case T_OPEN:
  730. case T_CREATE:
  731. if (send_fd(sock, retval) < 0) {
  732. return -1;
  733. }
  734. break;
  735. case T_MKNOD:
  736. case T_MKDIR:
  737. case T_SYMLINK:
  738. case T_LINK:
  739. case T_CHMOD:
  740. case T_CHOWN:
  741. case T_TRUNCATE:
  742. case T_UTIME:
  743. case T_RENAME:
  744. case T_REMOVE:
  745. case T_LSETXATTR:
  746. case T_LREMOVEXATTR:
  747. if (send_status(sock, out_iovec, retval) < 0) {
  748. return -1;
  749. }
  750. break;
  751. case T_LSTAT:
  752. case T_STATFS:
  753. case T_READLINK:
  754. case T_LGETXATTR:
  755. case T_LLISTXATTR:
  756. case T_GETVERSION:
  757. if (send_response(sock, out_iovec, retval) < 0) {
  758. return -1;
  759. }
  760. break;
  761. default:
  762. return -1;
  763. break;
  764. }
  765. return 0;
  766. }
  767. static int process_requests(int sock)
  768. {
  769. int flags;
  770. int size = 0;
  771. int retval = 0;
  772. uint64_t offset;
  773. ProxyHeader header;
  774. int mode, uid, gid;
  775. V9fsString name, value;
  776. struct timespec spec[2];
  777. V9fsString oldpath, path;
  778. struct iovec in_iovec, out_iovec;
  779. in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
  780. in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
  781. out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
  782. out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
  783. while (1) {
  784. /*
  785. * initialize the header type, so that we send
  786. * response to proper request type.
  787. */
  788. header.type = 0;
  789. retval = read_request(sock, &in_iovec, &header);
  790. if (retval < 0) {
  791. goto err_out;
  792. }
  793. switch (header.type) {
  794. case T_OPEN:
  795. retval = do_open(&in_iovec);
  796. break;
  797. case T_CREATE:
  798. retval = do_create(&in_iovec);
  799. break;
  800. case T_MKNOD:
  801. case T_MKDIR:
  802. case T_SYMLINK:
  803. retval = do_create_others(header.type, &in_iovec);
  804. break;
  805. case T_LINK:
  806. v9fs_string_init(&path);
  807. v9fs_string_init(&oldpath);
  808. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
  809. "ss", &oldpath, &path);
  810. if (retval > 0) {
  811. retval = link(oldpath.data, path.data);
  812. if (retval < 0) {
  813. retval = -errno;
  814. }
  815. }
  816. v9fs_string_free(&oldpath);
  817. v9fs_string_free(&path);
  818. break;
  819. case T_LSTAT:
  820. case T_STATFS:
  821. retval = do_stat(header.type, &in_iovec, &out_iovec);
  822. break;
  823. case T_READLINK:
  824. retval = do_readlink(&in_iovec, &out_iovec);
  825. break;
  826. case T_CHMOD:
  827. v9fs_string_init(&path);
  828. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
  829. "sd", &path, &mode);
  830. if (retval > 0) {
  831. retval = chmod(path.data, mode);
  832. if (retval < 0) {
  833. retval = -errno;
  834. }
  835. }
  836. v9fs_string_free(&path);
  837. break;
  838. case T_CHOWN:
  839. v9fs_string_init(&path);
  840. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
  841. &uid, &gid);
  842. if (retval > 0) {
  843. retval = lchown(path.data, uid, gid);
  844. if (retval < 0) {
  845. retval = -errno;
  846. }
  847. }
  848. v9fs_string_free(&path);
  849. break;
  850. case T_TRUNCATE:
  851. v9fs_string_init(&path);
  852. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
  853. &path, &offset);
  854. if (retval > 0) {
  855. retval = truncate(path.data, offset);
  856. if (retval < 0) {
  857. retval = -errno;
  858. }
  859. }
  860. v9fs_string_free(&path);
  861. break;
  862. case T_UTIME:
  863. v9fs_string_init(&path);
  864. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
  865. &spec[0].tv_sec, &spec[0].tv_nsec,
  866. &spec[1].tv_sec, &spec[1].tv_nsec);
  867. if (retval > 0) {
  868. retval = qemu_utimens(path.data, spec);
  869. if (retval < 0) {
  870. retval = -errno;
  871. }
  872. }
  873. v9fs_string_free(&path);
  874. break;
  875. case T_RENAME:
  876. v9fs_string_init(&path);
  877. v9fs_string_init(&oldpath);
  878. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
  879. "ss", &oldpath, &path);
  880. if (retval > 0) {
  881. retval = rename(oldpath.data, path.data);
  882. if (retval < 0) {
  883. retval = -errno;
  884. }
  885. }
  886. v9fs_string_free(&oldpath);
  887. v9fs_string_free(&path);
  888. break;
  889. case T_REMOVE:
  890. v9fs_string_init(&path);
  891. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
  892. if (retval > 0) {
  893. retval = remove(path.data);
  894. if (retval < 0) {
  895. retval = -errno;
  896. }
  897. }
  898. v9fs_string_free(&path);
  899. break;
  900. case T_LGETXATTR:
  901. case T_LLISTXATTR:
  902. retval = do_getxattr(header.type, &in_iovec, &out_iovec);
  903. break;
  904. case T_LSETXATTR:
  905. v9fs_string_init(&path);
  906. v9fs_string_init(&name);
  907. v9fs_string_init(&value);
  908. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
  909. &name, &value, &size, &flags);
  910. if (retval > 0) {
  911. retval = lsetxattr(path.data,
  912. name.data, value.data, size, flags);
  913. if (retval < 0) {
  914. retval = -errno;
  915. }
  916. }
  917. v9fs_string_free(&path);
  918. v9fs_string_free(&name);
  919. v9fs_string_free(&value);
  920. break;
  921. case T_LREMOVEXATTR:
  922. v9fs_string_init(&path);
  923. v9fs_string_init(&name);
  924. retval = proxy_unmarshal(&in_iovec,
  925. PROXY_HDR_SZ, "ss", &path, &name);
  926. if (retval > 0) {
  927. retval = lremovexattr(path.data, name.data);
  928. if (retval < 0) {
  929. retval = -errno;
  930. }
  931. }
  932. v9fs_string_free(&path);
  933. v9fs_string_free(&name);
  934. break;
  935. case T_GETVERSION:
  936. retval = do_getversion(&in_iovec, &out_iovec);
  937. break;
  938. default:
  939. goto err_out;
  940. break;
  941. }
  942. if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
  943. goto err_out;
  944. }
  945. }
  946. err_out:
  947. g_free(in_iovec.iov_base);
  948. g_free(out_iovec.iov_base);
  949. return -1;
  950. }
  951. int main(int argc, char **argv)
  952. {
  953. int sock;
  954. uid_t own_u;
  955. gid_t own_g;
  956. char *rpath = NULL;
  957. char *sock_name = NULL;
  958. struct stat stbuf;
  959. int c, option_index;
  960. #ifdef FS_IOC_GETVERSION
  961. int retval;
  962. struct statfs st_fs;
  963. #endif
  964. is_daemon = true;
  965. sock = -1;
  966. own_u = own_g = -1;
  967. while (1) {
  968. option_index = 0;
  969. c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
  970. &option_index);
  971. if (c == -1) {
  972. break;
  973. }
  974. switch (c) {
  975. case 'p':
  976. rpath = g_strdup(optarg);
  977. break;
  978. case 'n':
  979. is_daemon = false;
  980. break;
  981. case 'f':
  982. sock = atoi(optarg);
  983. break;
  984. case 's':
  985. sock_name = g_strdup(optarg);
  986. break;
  987. case 'u':
  988. own_u = atoi(optarg);
  989. break;
  990. case 'g':
  991. own_g = atoi(optarg);
  992. break;
  993. case '?':
  994. case 'h':
  995. default:
  996. usage(argv[0]);
  997. exit(EXIT_FAILURE);
  998. }
  999. }
  1000. /* Parameter validation */
  1001. if ((sock_name == NULL && sock == -1) || rpath == NULL) {
  1002. fprintf(stderr, "socket, socket descriptor or path not specified\n");
  1003. usage(argv[0]);
  1004. return -1;
  1005. }
  1006. if (sock_name && sock != -1) {
  1007. fprintf(stderr, "both named socket and socket descriptor specified\n");
  1008. usage(argv[0]);
  1009. exit(EXIT_FAILURE);
  1010. }
  1011. if (sock_name && (own_u == -1 || own_g == -1)) {
  1012. fprintf(stderr, "owner uid:gid not specified, ");
  1013. fprintf(stderr,
  1014. "owner uid:gid specifies who can access the socket file\n");
  1015. usage(argv[0]);
  1016. exit(EXIT_FAILURE);
  1017. }
  1018. if (lstat(rpath, &stbuf) < 0) {
  1019. fprintf(stderr, "invalid path \"%s\" specified, %s\n",
  1020. rpath, strerror(errno));
  1021. exit(EXIT_FAILURE);
  1022. }
  1023. if (!S_ISDIR(stbuf.st_mode)) {
  1024. fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
  1025. exit(EXIT_FAILURE);
  1026. }
  1027. if (is_daemon) {
  1028. if (daemon(0, 0) < 0) {
  1029. fprintf(stderr, "daemon call failed\n");
  1030. exit(EXIT_FAILURE);
  1031. }
  1032. openlog(PROGNAME, LOG_PID, LOG_DAEMON);
  1033. }
  1034. do_log(LOG_INFO, "Started\n");
  1035. if (sock_name) {
  1036. sock = proxy_socket(sock_name, own_u, own_g);
  1037. if (sock < 0) {
  1038. goto error;
  1039. }
  1040. }
  1041. get_version = false;
  1042. #ifdef FS_IOC_GETVERSION
  1043. /* check whether underlying FS support IOC_GETVERSION */
  1044. retval = statfs(rpath, &st_fs);
  1045. if (!retval) {
  1046. switch (st_fs.f_type) {
  1047. case EXT2_SUPER_MAGIC:
  1048. case BTRFS_SUPER_MAGIC:
  1049. case REISERFS_SUPER_MAGIC:
  1050. case XFS_SUPER_MAGIC:
  1051. get_version = true;
  1052. break;
  1053. }
  1054. }
  1055. #endif
  1056. if (chdir("/") < 0) {
  1057. do_perror("chdir");
  1058. goto error;
  1059. }
  1060. if (chroot(rpath) < 0) {
  1061. do_perror("chroot");
  1062. goto error;
  1063. }
  1064. umask(0);
  1065. if (init_capabilities() < 0) {
  1066. goto error;
  1067. }
  1068. process_requests(sock);
  1069. error:
  1070. do_log(LOG_INFO, "Done\n");
  1071. closelog();
  1072. return 0;
  1073. }