virtfs-proxy-helper.c 31 KB

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