virtfs-proxy-helper.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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 capabilities 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. g_assert(strlen(path) < sizeof(proxy.sun_path));
  680. sock = socket(AF_UNIX, SOCK_STREAM, 0);
  681. if (sock < 0) {
  682. do_perror("socket");
  683. return -1;
  684. }
  685. /* mask other part of mode bits */
  686. umask(7);
  687. proxy.sun_family = AF_UNIX;
  688. strcpy(proxy.sun_path, path);
  689. if (bind(sock, (struct sockaddr *)&proxy,
  690. sizeof(struct sockaddr_un)) < 0) {
  691. do_perror("bind");
  692. goto error;
  693. }
  694. if (chown(proxy.sun_path, uid, gid) < 0) {
  695. do_perror("chown");
  696. goto error;
  697. }
  698. if (listen(sock, 1) < 0) {
  699. do_perror("listen");
  700. goto error;
  701. }
  702. size = sizeof(qemu);
  703. client = accept(sock, (struct sockaddr *)&qemu, &size);
  704. if (client < 0) {
  705. do_perror("accept");
  706. goto error;
  707. }
  708. close(sock);
  709. return client;
  710. error:
  711. close(sock);
  712. return -1;
  713. }
  714. static void usage(char *prog)
  715. {
  716. fprintf(stderr, "usage: %s\n"
  717. " -p|--path <path> 9p path to export\n"
  718. " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
  719. " {-s|--socket <socketname> socket file used for communication\n"
  720. " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
  721. " access to this socket\n"
  722. " \tNote: -s & -f can not be used together\n"
  723. " [-n|--nodaemon] Run as a normal program\n",
  724. basename(prog));
  725. }
  726. static int process_reply(int sock, int type,
  727. struct iovec *out_iovec, int retval)
  728. {
  729. switch (type) {
  730. case T_OPEN:
  731. case T_CREATE:
  732. if (send_fd(sock, retval) < 0) {
  733. return -1;
  734. }
  735. break;
  736. case T_MKNOD:
  737. case T_MKDIR:
  738. case T_SYMLINK:
  739. case T_LINK:
  740. case T_CHMOD:
  741. case T_CHOWN:
  742. case T_TRUNCATE:
  743. case T_UTIME:
  744. case T_RENAME:
  745. case T_REMOVE:
  746. case T_LSETXATTR:
  747. case T_LREMOVEXATTR:
  748. if (send_status(sock, out_iovec, retval) < 0) {
  749. return -1;
  750. }
  751. break;
  752. case T_LSTAT:
  753. case T_STATFS:
  754. case T_READLINK:
  755. case T_LGETXATTR:
  756. case T_LLISTXATTR:
  757. case T_GETVERSION:
  758. if (send_response(sock, out_iovec, retval) < 0) {
  759. return -1;
  760. }
  761. break;
  762. default:
  763. return -1;
  764. break;
  765. }
  766. return 0;
  767. }
  768. static int process_requests(int sock)
  769. {
  770. int flags;
  771. int size = 0;
  772. int retval = 0;
  773. uint64_t offset;
  774. ProxyHeader header;
  775. int mode, uid, gid;
  776. V9fsString name, value;
  777. struct timespec spec[2];
  778. V9fsString oldpath, path;
  779. struct iovec in_iovec, out_iovec;
  780. in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
  781. in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
  782. out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
  783. out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
  784. while (1) {
  785. /*
  786. * initialize the header type, so that we send
  787. * response to proper request type.
  788. */
  789. header.type = 0;
  790. retval = read_request(sock, &in_iovec, &header);
  791. if (retval < 0) {
  792. goto err_out;
  793. }
  794. switch (header.type) {
  795. case T_OPEN:
  796. retval = do_open(&in_iovec);
  797. break;
  798. case T_CREATE:
  799. retval = do_create(&in_iovec);
  800. break;
  801. case T_MKNOD:
  802. case T_MKDIR:
  803. case T_SYMLINK:
  804. retval = do_create_others(header.type, &in_iovec);
  805. break;
  806. case T_LINK:
  807. v9fs_string_init(&path);
  808. v9fs_string_init(&oldpath);
  809. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
  810. "ss", &oldpath, &path);
  811. if (retval > 0) {
  812. retval = link(oldpath.data, path.data);
  813. if (retval < 0) {
  814. retval = -errno;
  815. }
  816. }
  817. v9fs_string_free(&oldpath);
  818. v9fs_string_free(&path);
  819. break;
  820. case T_LSTAT:
  821. case T_STATFS:
  822. retval = do_stat(header.type, &in_iovec, &out_iovec);
  823. break;
  824. case T_READLINK:
  825. retval = do_readlink(&in_iovec, &out_iovec);
  826. break;
  827. case T_CHMOD:
  828. v9fs_string_init(&path);
  829. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
  830. "sd", &path, &mode);
  831. if (retval > 0) {
  832. retval = chmod(path.data, mode);
  833. if (retval < 0) {
  834. retval = -errno;
  835. }
  836. }
  837. v9fs_string_free(&path);
  838. break;
  839. case T_CHOWN:
  840. v9fs_string_init(&path);
  841. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
  842. &uid, &gid);
  843. if (retval > 0) {
  844. retval = lchown(path.data, uid, gid);
  845. if (retval < 0) {
  846. retval = -errno;
  847. }
  848. }
  849. v9fs_string_free(&path);
  850. break;
  851. case T_TRUNCATE:
  852. v9fs_string_init(&path);
  853. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
  854. &path, &offset);
  855. if (retval > 0) {
  856. retval = truncate(path.data, offset);
  857. if (retval < 0) {
  858. retval = -errno;
  859. }
  860. }
  861. v9fs_string_free(&path);
  862. break;
  863. case T_UTIME:
  864. v9fs_string_init(&path);
  865. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
  866. &spec[0].tv_sec, &spec[0].tv_nsec,
  867. &spec[1].tv_sec, &spec[1].tv_nsec);
  868. if (retval > 0) {
  869. retval = qemu_utimens(path.data, spec);
  870. if (retval < 0) {
  871. retval = -errno;
  872. }
  873. }
  874. v9fs_string_free(&path);
  875. break;
  876. case T_RENAME:
  877. v9fs_string_init(&path);
  878. v9fs_string_init(&oldpath);
  879. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
  880. "ss", &oldpath, &path);
  881. if (retval > 0) {
  882. retval = rename(oldpath.data, path.data);
  883. if (retval < 0) {
  884. retval = -errno;
  885. }
  886. }
  887. v9fs_string_free(&oldpath);
  888. v9fs_string_free(&path);
  889. break;
  890. case T_REMOVE:
  891. v9fs_string_init(&path);
  892. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
  893. if (retval > 0) {
  894. retval = remove(path.data);
  895. if (retval < 0) {
  896. retval = -errno;
  897. }
  898. }
  899. v9fs_string_free(&path);
  900. break;
  901. case T_LGETXATTR:
  902. case T_LLISTXATTR:
  903. retval = do_getxattr(header.type, &in_iovec, &out_iovec);
  904. break;
  905. case T_LSETXATTR:
  906. v9fs_string_init(&path);
  907. v9fs_string_init(&name);
  908. v9fs_string_init(&value);
  909. retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
  910. &name, &value, &size, &flags);
  911. if (retval > 0) {
  912. retval = lsetxattr(path.data,
  913. name.data, value.data, size, flags);
  914. if (retval < 0) {
  915. retval = -errno;
  916. }
  917. }
  918. v9fs_string_free(&path);
  919. v9fs_string_free(&name);
  920. v9fs_string_free(&value);
  921. break;
  922. case T_LREMOVEXATTR:
  923. v9fs_string_init(&path);
  924. v9fs_string_init(&name);
  925. retval = proxy_unmarshal(&in_iovec,
  926. PROXY_HDR_SZ, "ss", &path, &name);
  927. if (retval > 0) {
  928. retval = lremovexattr(path.data, name.data);
  929. if (retval < 0) {
  930. retval = -errno;
  931. }
  932. }
  933. v9fs_string_free(&path);
  934. v9fs_string_free(&name);
  935. break;
  936. case T_GETVERSION:
  937. retval = do_getversion(&in_iovec, &out_iovec);
  938. break;
  939. default:
  940. goto err_out;
  941. break;
  942. }
  943. if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
  944. goto err_out;
  945. }
  946. }
  947. err_out:
  948. g_free(in_iovec.iov_base);
  949. g_free(out_iovec.iov_base);
  950. return -1;
  951. }
  952. int main(int argc, char **argv)
  953. {
  954. int sock;
  955. uid_t own_u;
  956. gid_t own_g;
  957. char *rpath = NULL;
  958. char *sock_name = NULL;
  959. struct stat stbuf;
  960. int c, option_index;
  961. #ifdef FS_IOC_GETVERSION
  962. int retval;
  963. struct statfs st_fs;
  964. #endif
  965. is_daemon = true;
  966. sock = -1;
  967. own_u = own_g = -1;
  968. while (1) {
  969. option_index = 0;
  970. c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
  971. &option_index);
  972. if (c == -1) {
  973. break;
  974. }
  975. switch (c) {
  976. case 'p':
  977. rpath = g_strdup(optarg);
  978. break;
  979. case 'n':
  980. is_daemon = false;
  981. break;
  982. case 'f':
  983. sock = atoi(optarg);
  984. break;
  985. case 's':
  986. sock_name = g_strdup(optarg);
  987. break;
  988. case 'u':
  989. own_u = atoi(optarg);
  990. break;
  991. case 'g':
  992. own_g = atoi(optarg);
  993. break;
  994. case '?':
  995. case 'h':
  996. default:
  997. usage(argv[0]);
  998. exit(EXIT_FAILURE);
  999. }
  1000. }
  1001. /* Parameter validation */
  1002. if ((sock_name == NULL && sock == -1) || rpath == NULL) {
  1003. fprintf(stderr, "socket, socket descriptor or path not specified\n");
  1004. usage(argv[0]);
  1005. return -1;
  1006. }
  1007. if (sock_name && sock != -1) {
  1008. fprintf(stderr, "both named socket and socket descriptor specified\n");
  1009. usage(argv[0]);
  1010. exit(EXIT_FAILURE);
  1011. }
  1012. if (sock_name && (own_u == -1 || own_g == -1)) {
  1013. fprintf(stderr, "owner uid:gid not specified, ");
  1014. fprintf(stderr,
  1015. "owner uid:gid specifies who can access the socket file\n");
  1016. usage(argv[0]);
  1017. exit(EXIT_FAILURE);
  1018. }
  1019. if (lstat(rpath, &stbuf) < 0) {
  1020. fprintf(stderr, "invalid path \"%s\" specified, %s\n",
  1021. rpath, strerror(errno));
  1022. exit(EXIT_FAILURE);
  1023. }
  1024. if (!S_ISDIR(stbuf.st_mode)) {
  1025. fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
  1026. exit(EXIT_FAILURE);
  1027. }
  1028. if (is_daemon) {
  1029. if (daemon(0, 0) < 0) {
  1030. fprintf(stderr, "daemon call failed\n");
  1031. exit(EXIT_FAILURE);
  1032. }
  1033. openlog(PROGNAME, LOG_PID, LOG_DAEMON);
  1034. }
  1035. do_log(LOG_INFO, "Started\n");
  1036. if (sock_name) {
  1037. sock = proxy_socket(sock_name, own_u, own_g);
  1038. if (sock < 0) {
  1039. goto error;
  1040. }
  1041. }
  1042. get_version = false;
  1043. #ifdef FS_IOC_GETVERSION
  1044. /* check whether underlying FS support IOC_GETVERSION */
  1045. retval = statfs(rpath, &st_fs);
  1046. if (!retval) {
  1047. switch (st_fs.f_type) {
  1048. case EXT2_SUPER_MAGIC:
  1049. case BTRFS_SUPER_MAGIC:
  1050. case REISERFS_SUPER_MAGIC:
  1051. case XFS_SUPER_MAGIC:
  1052. get_version = true;
  1053. break;
  1054. }
  1055. }
  1056. #endif
  1057. if (chdir("/") < 0) {
  1058. do_perror("chdir");
  1059. goto error;
  1060. }
  1061. if (chroot(rpath) < 0) {
  1062. do_perror("chroot");
  1063. goto error;
  1064. }
  1065. umask(0);
  1066. if (init_capabilities() < 0) {
  1067. goto error;
  1068. }
  1069. process_requests(sock);
  1070. error:
  1071. do_log(LOG_INFO, "Done\n");
  1072. closelog();
  1073. return 0;
  1074. }