2 * Virtio 9p Posix callback
4 * Copyright IBM, Corp. 2010
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "virtio-9p.h"
15 #include <arpa/inet.h>
18 #include <sys/socket.h>
20 #include <attr/xattr.h>
22 static const char *rpath(FsContext *ctx, const char *path)
24 /* FIXME: so wrong... */
25 static char buffer[4096];
26 snprintf(buffer, sizeof(buffer), "%s/%s", ctx->fs_root, path);
31 static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf)
34 err = lstat(rpath(fs_ctx, path), stbuf);
38 if (fs_ctx->fs_sm == SM_MAPPED) {
39 /* Actual credentials are part of extended attrs */
44 if (getxattr(rpath(fs_ctx, path), "user.virtfs.uid", &tmp_uid,
46 stbuf->st_uid = tmp_uid;
48 if (getxattr(rpath(fs_ctx, path), "user.virtfs.gid", &tmp_gid,
50 stbuf->st_gid = tmp_gid;
52 if (getxattr(rpath(fs_ctx, path), "user.virtfs.mode", &tmp_mode,
53 sizeof(mode_t)) > 0) {
54 stbuf->st_mode = tmp_mode;
56 if (getxattr(rpath(fs_ctx, path), "user.virtfs.rdev", &tmp_dev,
58 stbuf->st_rdev = tmp_dev;
64 static int local_set_xattr(const char *path, FsCred *credp)
67 if (credp->fc_uid != -1) {
68 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
74 if (credp->fc_gid != -1) {
75 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
81 if (credp->fc_mode != -1) {
82 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
88 if (credp->fc_rdev != -1) {
89 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
98 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
101 if (chmod(rpath(fs_ctx, path), credp->fc_mode & 07777) < 0) {
104 if (lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid) < 0) {
106 * If we fail to change ownership and if we are
107 * using security model none. Ignore the error
109 if (fs_ctx->fs_sm != SM_NONE) {
116 static ssize_t local_readlink(FsContext *fs_ctx, const char *path,
117 char *buf, size_t bufsz)
120 if (fs_ctx->fs_sm == SM_MAPPED) {
122 fd = open(rpath(fs_ctx, path), O_RDONLY);
127 tsize = read(fd, (void *)buf, bufsz);
128 } while (tsize == -1 && errno == EINTR);
131 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
132 (fs_ctx->fs_sm == SM_NONE)) {
133 tsize = readlink(rpath(fs_ctx, path), buf, bufsz);
138 static int local_close(FsContext *ctx, int fd)
143 static int local_closedir(FsContext *ctx, DIR *dir)
145 return closedir(dir);
148 static int local_open(FsContext *ctx, const char *path, int flags)
150 return open(rpath(ctx, path), flags);
153 static DIR *local_opendir(FsContext *ctx, const char *path)
155 return opendir(rpath(ctx, path));
158 static void local_rewinddir(FsContext *ctx, DIR *dir)
160 return rewinddir(dir);
163 static off_t local_telldir(FsContext *ctx, DIR *dir)
168 static struct dirent *local_readdir(FsContext *ctx, DIR *dir)
173 static void local_seekdir(FsContext *ctx, DIR *dir, off_t off)
175 return seekdir(dir, off);
178 static ssize_t local_readv(FsContext *ctx, int fd, const struct iovec *iov,
181 return readv(fd, iov, iovcnt);
184 static off_t local_lseek(FsContext *ctx, int fd, off_t offset, int whence)
186 return lseek(fd, offset, whence);
189 static ssize_t local_writev(FsContext *ctx, int fd, const struct iovec *iov,
192 return writev(fd, iov, iovcnt);
195 static int local_chmod(FsContext *fs_ctx, const char *path, FsCred *credp)
197 if (fs_ctx->fs_sm == SM_MAPPED) {
198 return local_set_xattr(rpath(fs_ctx, path), credp);
199 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
200 (fs_ctx->fs_sm == SM_NONE)) {
201 return chmod(rpath(fs_ctx, path), credp->fc_mode);
206 static int local_mknod(FsContext *fs_ctx, const char *path, FsCred *credp)
211 /* Determine the security model */
212 if (fs_ctx->fs_sm == SM_MAPPED) {
213 err = mknod(rpath(fs_ctx, path), SM_LOCAL_MODE_BITS|S_IFREG, 0);
217 local_set_xattr(rpath(fs_ctx, path), credp);
222 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
223 (fs_ctx->fs_sm == SM_NONE)) {
224 err = mknod(rpath(fs_ctx, path), credp->fc_mode, credp->fc_rdev);
228 err = local_post_create_passthrough(fs_ctx, path, credp);
237 remove(rpath(fs_ctx, path));
242 static int local_mkdir(FsContext *fs_ctx, const char *path, FsCred *credp)
247 /* Determine the security model */
248 if (fs_ctx->fs_sm == SM_MAPPED) {
249 err = mkdir(rpath(fs_ctx, path), SM_LOCAL_DIR_MODE_BITS);
253 credp->fc_mode = credp->fc_mode|S_IFDIR;
254 err = local_set_xattr(rpath(fs_ctx, path), credp);
259 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
260 (fs_ctx->fs_sm == SM_NONE)) {
261 err = mkdir(rpath(fs_ctx, path), credp->fc_mode);
265 err = local_post_create_passthrough(fs_ctx, path, credp);
274 remove(rpath(fs_ctx, path));
279 static int local_fstat(FsContext *fs_ctx, int fd, struct stat *stbuf)
282 err = fstat(fd, stbuf);
286 if (fs_ctx->fs_sm == SM_MAPPED) {
287 /* Actual credentials are part of extended attrs */
293 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
294 stbuf->st_uid = tmp_uid;
296 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
297 stbuf->st_gid = tmp_gid;
299 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
300 stbuf->st_mode = tmp_mode;
302 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
303 stbuf->st_rdev = tmp_dev;
309 static int local_open2(FsContext *fs_ctx, const char *path, int flags,
316 /* Determine the security model */
317 if (fs_ctx->fs_sm == SM_MAPPED) {
318 fd = open(rpath(fs_ctx, path), flags, SM_LOCAL_MODE_BITS);
322 credp->fc_mode = credp->fc_mode|S_IFREG;
323 /* Set cleint credentials in xattr */
324 err = local_set_xattr(rpath(fs_ctx, path), credp);
329 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
330 (fs_ctx->fs_sm == SM_NONE)) {
331 fd = open(rpath(fs_ctx, path), flags, credp->fc_mode);
335 err = local_post_create_passthrough(fs_ctx, path, credp);
345 remove(rpath(fs_ctx, path));
351 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
352 const char *newpath, FsCred *credp)
357 /* Determine the security model */
358 if (fs_ctx->fs_sm == SM_MAPPED) {
360 ssize_t oldpath_size, write_size;
361 fd = open(rpath(fs_ctx, newpath), O_CREAT|O_EXCL|O_RDWR,
366 /* Write the oldpath (target) to the file. */
367 oldpath_size = strlen(oldpath) + 1;
369 write_size = write(fd, (void *)oldpath, oldpath_size);
370 } while (write_size == -1 && errno == EINTR);
372 if (write_size != oldpath_size) {
379 /* Set cleint credentials in symlink's xattr */
380 credp->fc_mode = credp->fc_mode|S_IFLNK;
381 err = local_set_xattr(rpath(fs_ctx, newpath), credp);
386 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
387 (fs_ctx->fs_sm == SM_NONE)) {
388 err = symlink(oldpath, rpath(fs_ctx, newpath));
392 err = lchown(rpath(fs_ctx, newpath), credp->fc_uid, credp->fc_gid);
395 * If we fail to change ownership and if we are
396 * using security model none. Ignore the error
398 if (fs_ctx->fs_sm != SM_NONE) {
408 remove(rpath(fs_ctx, newpath));
413 static int local_link(FsContext *ctx, const char *oldpath, const char *newpath)
415 char *tmp = qemu_strdup(rpath(ctx, oldpath));
422 err = link(tmp, rpath(ctx, newpath));
436 static int local_truncate(FsContext *ctx, const char *path, off_t size)
438 return truncate(rpath(ctx, path), size);
441 static int local_rename(FsContext *ctx, const char *oldpath,
447 tmp = qemu_strdup(rpath(ctx, oldpath));
449 err = rename(tmp, rpath(ctx, newpath));
462 static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp)
464 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
465 (fs_ctx->fs_sm == SM_PASSTHROUGH)) {
466 return lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
467 } else if (fs_ctx->fs_sm == SM_MAPPED) {
468 return local_set_xattr(rpath(fs_ctx, path), credp);
469 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
470 (fs_ctx->fs_sm == SM_NONE)) {
471 return lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
476 static int local_utimensat(FsContext *s, const char *path,
477 const struct timespec *buf)
479 return utimensat(AT_FDCWD, rpath(s, path), buf, AT_SYMLINK_NOFOLLOW);
482 static int local_remove(FsContext *ctx, const char *path)
484 return remove(rpath(ctx, path));
487 static int local_fsync(FsContext *ctx, int fd)
492 static int local_statfs(FsContext *s, const char *path, struct statfs *stbuf)
494 return statfs(rpath(s, path), stbuf);
497 static ssize_t local_lgetxattr(FsContext *ctx, const char *path,
498 const char *name, void *value, size_t size)
500 if ((ctx->fs_sm == SM_MAPPED) &&
501 (strncmp(name, "user.virtfs.", 12) == 0)) {
503 * Don't allow fetch of user.virtfs namesapce
504 * in case of mapped security
510 return lgetxattr(rpath(ctx, path), name, value, size);
513 static ssize_t local_llistxattr(FsContext *ctx, const char *path,
514 void *value, size_t size)
517 ssize_t actual_len = 0;
518 char *orig_value, *orig_value_start;
519 char *temp_value, *temp_value_start;
520 ssize_t xattr_len, parsed_len = 0, attr_len;
522 if (ctx->fs_sm != SM_MAPPED) {
523 return llistxattr(rpath(ctx, path), value, size);
526 /* Get the actual len */
527 xattr_len = llistxattr(rpath(ctx, path), value, 0);
529 /* Now fetch the xattr and find the actual size */
530 orig_value = qemu_malloc(xattr_len);
531 xattr_len = llistxattr(rpath(ctx, path), orig_value, xattr_len);
534 * For mapped security model drop user.virtfs namespace
537 temp_value = qemu_mallocz(xattr_len);
538 temp_value_start = temp_value;
539 orig_value_start = orig_value;
540 while (xattr_len > parsed_len) {
541 attr_len = strlen(orig_value) + 1;
542 if (strncmp(orig_value, "user.virtfs.", 12) != 0) {
543 /* Copy this entry */
544 strcat(temp_value, orig_value);
545 temp_value += attr_len;
546 actual_len += attr_len;
548 parsed_len += attr_len;
549 orig_value += attr_len;
554 } else if (size >= actual_len) {
555 /* now copy the parsed attribute list back */
556 memset(value, 0, size);
557 memcpy(value, temp_value_start, actual_len);
564 qemu_free(orig_value_start);
565 qemu_free(temp_value_start);
569 static int local_lsetxattr(FsContext *ctx, const char *path, const char *name,
570 void *value, size_t size, int flags)
572 if ((ctx->fs_sm == SM_MAPPED) &&
573 (strncmp(name, "user.virtfs.", 12) == 0)) {
575 * Don't allow fetch of user.virtfs namesapce
576 * in case of mapped security
581 return lsetxattr(rpath(ctx, path), name, value, size, flags);
584 static int local_lremovexattr(FsContext *ctx,
585 const char *path, const char *name)
587 if ((ctx->fs_sm == SM_MAPPED) &&
588 (strncmp(name, "user.virtfs.", 12) == 0)) {
590 * Don't allow fetch of user.virtfs namesapce
591 * in case of mapped security
596 return lremovexattr(rpath(ctx, path), name);
600 FileOperations local_ops = {
601 .lstat = local_lstat,
602 .readlink = local_readlink,
603 .close = local_close,
604 .closedir = local_closedir,
606 .opendir = local_opendir,
607 .rewinddir = local_rewinddir,
608 .telldir = local_telldir,
609 .readdir = local_readdir,
610 .seekdir = local_seekdir,
611 .readv = local_readv,
612 .lseek = local_lseek,
613 .writev = local_writev,
614 .chmod = local_chmod,
615 .mknod = local_mknod,
616 .mkdir = local_mkdir,
617 .fstat = local_fstat,
618 .open2 = local_open2,
619 .symlink = local_symlink,
621 .truncate = local_truncate,
622 .rename = local_rename,
623 .chown = local_chown,
624 .utimensat = local_utimensat,
625 .remove = local_remove,
626 .fsync = local_fsync,
627 .statfs = local_statfs,
628 .lgetxattr = local_lgetxattr,
629 .llistxattr = local_llistxattr,
630 .lsetxattr = local_lsetxattr,
631 .lremovexattr = local_lremovexattr,