os-posix: set groups properly for -runas
[qemu/v9fs2.git] / hw / 9pfs / virtio-9p-local.c
blob77904c37bd458cc8c3005398a10a70c8e093466e
1 /*
2 * Virtio 9p Posix callback
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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 "hw/virtio.h"
15 #include "virtio-9p.h"
16 #include "virtio-9p-xattr.h"
17 #include <arpa/inet.h>
18 #include <pwd.h>
19 #include <grp.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <attr/xattr.h>
25 static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf)
27 int err;
28 char buffer[PATH_MAX];
29 err = lstat(rpath(fs_ctx, path, buffer), stbuf);
30 if (err) {
31 return err;
33 if (fs_ctx->fs_sm == SM_MAPPED) {
34 /* Actual credentials are part of extended attrs */
35 uid_t tmp_uid;
36 gid_t tmp_gid;
37 mode_t tmp_mode;
38 dev_t tmp_dev;
39 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.uid", &tmp_uid,
40 sizeof(uid_t)) > 0) {
41 stbuf->st_uid = tmp_uid;
43 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.gid", &tmp_gid,
44 sizeof(gid_t)) > 0) {
45 stbuf->st_gid = tmp_gid;
47 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.mode",
48 &tmp_mode, sizeof(mode_t)) > 0) {
49 stbuf->st_mode = tmp_mode;
51 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.rdev", &tmp_dev,
52 sizeof(dev_t)) > 0) {
53 stbuf->st_rdev = tmp_dev;
56 return err;
59 static int local_set_xattr(const char *path, FsCred *credp)
61 int err;
62 if (credp->fc_uid != -1) {
63 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
64 0);
65 if (err) {
66 return err;
69 if (credp->fc_gid != -1) {
70 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
71 0);
72 if (err) {
73 return err;
76 if (credp->fc_mode != -1) {
77 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
78 sizeof(mode_t), 0);
79 if (err) {
80 return err;
83 if (credp->fc_rdev != -1) {
84 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
85 sizeof(dev_t), 0);
86 if (err) {
87 return err;
90 return 0;
93 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
94 FsCred *credp)
96 char buffer[PATH_MAX];
97 if (chmod(rpath(fs_ctx, path, buffer), credp->fc_mode & 07777) < 0) {
98 return -1;
100 if (lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
101 credp->fc_gid) < 0) {
103 * If we fail to change ownership and if we are
104 * using security model none. Ignore the error
106 if (fs_ctx->fs_sm != SM_NONE) {
107 return -1;
110 return 0;
113 static ssize_t local_readlink(FsContext *fs_ctx, const char *path,
114 char *buf, size_t bufsz)
116 ssize_t tsize = -1;
117 char buffer[PATH_MAX];
118 if (fs_ctx->fs_sm == SM_MAPPED) {
119 int fd;
120 fd = open(rpath(fs_ctx, path, buffer), O_RDONLY);
121 if (fd == -1) {
122 return -1;
124 do {
125 tsize = read(fd, (void *)buf, bufsz);
126 } while (tsize == -1 && errno == EINTR);
127 close(fd);
128 return tsize;
129 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
130 (fs_ctx->fs_sm == SM_NONE)) {
131 tsize = readlink(rpath(fs_ctx, path, buffer), buf, bufsz);
133 return tsize;
136 static int local_close(FsContext *ctx, int fd)
138 return close(fd);
141 static int local_closedir(FsContext *ctx, DIR *dir)
143 return closedir(dir);
146 static int local_open(FsContext *ctx, const char *path, int flags)
148 char buffer[PATH_MAX];
149 return open(rpath(ctx, path, buffer), flags);
152 static DIR *local_opendir(FsContext *ctx, const char *path)
154 char buffer[PATH_MAX];
155 return opendir(rpath(ctx, path, buffer));
158 static void local_rewinddir(FsContext *ctx, DIR *dir)
160 return rewinddir(dir);
163 static off_t local_telldir(FsContext *ctx, DIR *dir)
165 return telldir(dir);
168 static struct dirent *local_readdir(FsContext *ctx, DIR *dir)
170 return readdir(dir);
173 static void local_seekdir(FsContext *ctx, DIR *dir, off_t off)
175 return seekdir(dir, off);
178 static ssize_t local_preadv(FsContext *ctx, int fd, const struct iovec *iov,
179 int iovcnt, off_t offset)
181 #ifdef CONFIG_PREADV
182 return preadv(fd, iov, iovcnt, offset);
183 #else
184 int err = lseek(fd, offset, SEEK_SET);
185 if (err == -1) {
186 return err;
187 } else {
188 return readv(fd, iov, iovcnt);
190 #endif
193 static ssize_t local_pwritev(FsContext *ctx, int fd, const struct iovec *iov,
194 int iovcnt, off_t offset)
196 #ifdef CONFIG_PREADV
197 return pwritev(fd, iov, iovcnt, offset);
198 #else
199 int err = lseek(fd, offset, SEEK_SET);
200 if (err == -1) {
201 return err;
202 } else {
203 return writev(fd, iov, iovcnt);
205 #endif
208 static int local_chmod(FsContext *fs_ctx, const char *path, FsCred *credp)
210 char buffer[PATH_MAX];
211 if (fs_ctx->fs_sm == SM_MAPPED) {
212 return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
213 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
214 (fs_ctx->fs_sm == SM_NONE)) {
215 return chmod(rpath(fs_ctx, path, buffer), credp->fc_mode);
217 return -1;
220 static int local_mknod(FsContext *fs_ctx, const char *path, FsCred *credp)
222 int err = -1;
223 int serrno = 0;
224 char buffer[PATH_MAX];
226 /* Determine the security model */
227 if (fs_ctx->fs_sm == SM_MAPPED) {
228 err = mknod(rpath(fs_ctx, path, buffer),
229 SM_LOCAL_MODE_BITS|S_IFREG, 0);
230 if (err == -1) {
231 return err;
233 local_set_xattr(rpath(fs_ctx, path, buffer), credp);
234 if (err == -1) {
235 serrno = errno;
236 goto err_end;
238 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
239 (fs_ctx->fs_sm == SM_NONE)) {
240 err = mknod(rpath(fs_ctx, path, buffer), credp->fc_mode,
241 credp->fc_rdev);
242 if (err == -1) {
243 return err;
245 err = local_post_create_passthrough(fs_ctx, path, credp);
246 if (err == -1) {
247 serrno = errno;
248 goto err_end;
251 return err;
253 err_end:
254 remove(rpath(fs_ctx, path, buffer));
255 errno = serrno;
256 return err;
259 static int local_mkdir(FsContext *fs_ctx, const char *path, FsCred *credp)
261 int err = -1;
262 int serrno = 0;
263 char buffer[PATH_MAX];
265 /* Determine the security model */
266 if (fs_ctx->fs_sm == SM_MAPPED) {
267 err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS);
268 if (err == -1) {
269 return err;
271 credp->fc_mode = credp->fc_mode|S_IFDIR;
272 err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
273 if (err == -1) {
274 serrno = errno;
275 goto err_end;
277 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
278 (fs_ctx->fs_sm == SM_NONE)) {
279 err = mkdir(rpath(fs_ctx, path, buffer), credp->fc_mode);
280 if (err == -1) {
281 return err;
283 err = local_post_create_passthrough(fs_ctx, path, credp);
284 if (err == -1) {
285 serrno = errno;
286 goto err_end;
289 return err;
291 err_end:
292 remove(rpath(fs_ctx, path, buffer));
293 errno = serrno;
294 return err;
297 static int local_fstat(FsContext *fs_ctx, int fd, struct stat *stbuf)
299 int err;
300 err = fstat(fd, stbuf);
301 if (err) {
302 return err;
304 if (fs_ctx->fs_sm == SM_MAPPED) {
305 /* Actual credentials are part of extended attrs */
306 uid_t tmp_uid;
307 gid_t tmp_gid;
308 mode_t tmp_mode;
309 dev_t tmp_dev;
311 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
312 stbuf->st_uid = tmp_uid;
314 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
315 stbuf->st_gid = tmp_gid;
317 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
318 stbuf->st_mode = tmp_mode;
320 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
321 stbuf->st_rdev = tmp_dev;
324 return err;
327 static int local_open2(FsContext *fs_ctx, const char *path, int flags,
328 FsCred *credp)
330 int fd = -1;
331 int err = -1;
332 int serrno = 0;
333 char buffer[PATH_MAX];
335 /* Determine the security model */
336 if (fs_ctx->fs_sm == SM_MAPPED) {
337 fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS);
338 if (fd == -1) {
339 return fd;
341 credp->fc_mode = credp->fc_mode|S_IFREG;
342 /* Set cleint credentials in xattr */
343 err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
344 if (err == -1) {
345 serrno = errno;
346 goto err_end;
348 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
349 (fs_ctx->fs_sm == SM_NONE)) {
350 fd = open(rpath(fs_ctx, path, buffer), flags, credp->fc_mode);
351 if (fd == -1) {
352 return fd;
354 err = local_post_create_passthrough(fs_ctx, path, credp);
355 if (err == -1) {
356 serrno = errno;
357 goto err_end;
360 return fd;
362 err_end:
363 close(fd);
364 remove(rpath(fs_ctx, path, buffer));
365 errno = serrno;
366 return err;
370 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
371 const char *newpath, FsCred *credp)
373 int err = -1;
374 int serrno = 0;
375 char buffer[PATH_MAX];
377 /* Determine the security model */
378 if (fs_ctx->fs_sm == SM_MAPPED) {
379 int fd;
380 ssize_t oldpath_size, write_size;
381 fd = open(rpath(fs_ctx, newpath, buffer), O_CREAT|O_EXCL|O_RDWR,
382 SM_LOCAL_MODE_BITS);
383 if (fd == -1) {
384 return fd;
386 /* Write the oldpath (target) to the file. */
387 oldpath_size = strlen(oldpath);
388 do {
389 write_size = write(fd, (void *)oldpath, oldpath_size);
390 } while (write_size == -1 && errno == EINTR);
392 if (write_size != oldpath_size) {
393 serrno = errno;
394 close(fd);
395 err = -1;
396 goto err_end;
398 close(fd);
399 /* Set cleint credentials in symlink's xattr */
400 credp->fc_mode = credp->fc_mode|S_IFLNK;
401 err = local_set_xattr(rpath(fs_ctx, newpath, buffer), credp);
402 if (err == -1) {
403 serrno = errno;
404 goto err_end;
406 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
407 (fs_ctx->fs_sm == SM_NONE)) {
408 err = symlink(oldpath, rpath(fs_ctx, newpath, buffer));
409 if (err) {
410 return err;
412 err = lchown(rpath(fs_ctx, newpath, buffer), credp->fc_uid,
413 credp->fc_gid);
414 if (err == -1) {
416 * If we fail to change ownership and if we are
417 * using security model none. Ignore the error
419 if (fs_ctx->fs_sm != SM_NONE) {
420 serrno = errno;
421 goto err_end;
422 } else
423 err = 0;
426 return err;
428 err_end:
429 remove(rpath(fs_ctx, newpath, buffer));
430 errno = serrno;
431 return err;
434 static int local_link(FsContext *ctx, const char *oldpath, const char *newpath)
436 char buffer[PATH_MAX], buffer1[PATH_MAX];
438 return link(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1));
441 static int local_truncate(FsContext *ctx, const char *path, off_t size)
443 char buffer[PATH_MAX];
444 return truncate(rpath(ctx, path, buffer), size);
447 static int local_rename(FsContext *ctx, const char *oldpath,
448 const char *newpath)
450 char buffer[PATH_MAX], buffer1[PATH_MAX];
452 return rename(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1));
455 static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp)
457 char buffer[PATH_MAX];
458 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
459 (fs_ctx->fs_sm == SM_PASSTHROUGH)) {
460 return lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
461 credp->fc_gid);
462 } else if (fs_ctx->fs_sm == SM_MAPPED) {
463 return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
464 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
465 (fs_ctx->fs_sm == SM_NONE)) {
466 return lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
467 credp->fc_gid);
469 return -1;
472 static int local_utimensat(FsContext *s, const char *path,
473 const struct timespec *buf)
475 char buffer[PATH_MAX];
476 return qemu_utimensat(AT_FDCWD, rpath(s, path, buffer), buf,
477 AT_SYMLINK_NOFOLLOW);
480 static int local_remove(FsContext *ctx, const char *path)
482 char buffer[PATH_MAX];
483 return remove(rpath(ctx, path, buffer));
486 static int local_fsync(FsContext *ctx, int fd, int datasync)
488 if (datasync) {
489 return qemu_fdatasync(fd);
490 } else {
491 return fsync(fd);
495 static int local_statfs(FsContext *s, const char *path, struct statfs *stbuf)
497 char buffer[PATH_MAX];
498 return statfs(rpath(s, path, buffer), stbuf);
501 static ssize_t local_lgetxattr(FsContext *ctx, const char *path,
502 const char *name, void *value, size_t size)
504 return v9fs_get_xattr(ctx, path, name, value, size);
507 static ssize_t local_llistxattr(FsContext *ctx, const char *path,
508 void *value, size_t size)
510 return v9fs_list_xattr(ctx, path, value, size);
513 static int local_lsetxattr(FsContext *ctx, const char *path, const char *name,
514 void *value, size_t size, int flags)
516 return v9fs_set_xattr(ctx, path, name, value, size, flags);
519 static int local_lremovexattr(FsContext *ctx,
520 const char *path, const char *name)
522 return v9fs_remove_xattr(ctx, path, name);
526 FileOperations local_ops = {
527 .lstat = local_lstat,
528 .readlink = local_readlink,
529 .close = local_close,
530 .closedir = local_closedir,
531 .open = local_open,
532 .opendir = local_opendir,
533 .rewinddir = local_rewinddir,
534 .telldir = local_telldir,
535 .readdir = local_readdir,
536 .seekdir = local_seekdir,
537 .preadv = local_preadv,
538 .pwritev = local_pwritev,
539 .chmod = local_chmod,
540 .mknod = local_mknod,
541 .mkdir = local_mkdir,
542 .fstat = local_fstat,
543 .open2 = local_open2,
544 .symlink = local_symlink,
545 .link = local_link,
546 .truncate = local_truncate,
547 .rename = local_rename,
548 .chown = local_chown,
549 .utimensat = local_utimensat,
550 .remove = local_remove,
551 .fsync = local_fsync,
552 .statfs = local_statfs,
553 .lgetxattr = local_lgetxattr,
554 .llistxattr = local_llistxattr,
555 .lsetxattr = local_lsetxattr,
556 .lremovexattr = local_lremovexattr,