Revision 758e8e38 hw/virtio-9p-local.c

b/hw/virtio-9p-local.c
17 17
#include <grp.h>
18 18
#include <sys/socket.h>
19 19
#include <sys/un.h>
20
#include <attr/xattr.h>
20 21

  
21 22
static const char *rpath(FsContext *ctx, const char *path)
22 23
{
......
31 32
    return lstat(rpath(ctx, path), stbuf);
32 33
}
33 34

  
34
static int local_setuid(FsContext *ctx, uid_t uid)
35
static int local_set_xattr(const char *path, FsCred *credp)
35 36
{
36
    struct passwd *pw;
37
    gid_t groups[33];
38
    int ngroups;
39
    static uid_t cur_uid = -1;
40

  
41
    if (cur_uid == uid) {
42
        return 0;
43
    }
44

  
45
    if (setreuid(0, 0)) {
46
        return -1;
47
    }
48

  
49
    pw = getpwuid(uid);
50
    if (pw == NULL) {
51
        return -1;
52
    }
53

  
54
    ngroups = 33;
55
    if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1) {
56
        return -1;
37
    int err;
38
    if (credp->fc_uid != -1) {
39
        err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
40
                0);
41
        if (err) {
42
            return err;
43
        }
57 44
    }
58

  
59
    if (setgroups(ngroups, groups)) {
60
        return -1;
45
    if (credp->fc_gid != -1) {
46
        err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
47
                0);
48
        if (err) {
49
            return err;
50
        }
61 51
    }
62

  
63
    if (setregid(-1, pw->pw_gid)) {
64
        return -1;
52
    if (credp->fc_mode != -1) {
53
        err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
54
                sizeof(mode_t), 0);
55
        if (err) {
56
            return err;
57
        }
65 58
    }
66

  
67
    if (setreuid(-1, uid)) {
68
        return -1;
59
    if (credp->fc_rdev != -1) {
60
        err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
61
                sizeof(dev_t), 0);
62
        if (err) {
63
            return err;
64
        }
69 65
    }
70

  
71
    cur_uid = uid;
72

  
73 66
    return 0;
74 67
}
75 68

  
......
183 176
    return open(rpath(ctx, path), flags, mode);
184 177
}
185 178

  
179

  
186 180
static int local_symlink(FsContext *ctx, const char *oldpath,
187 181
                            const char *newpath)
188 182
{
......
259 253

  
260 254
static int local_fsync(FsContext *ctx, int fd)
261 255
{
256
    if (0) /* Just to supress the warning. Will be removed in next patch. */
257
        (void)local_set_xattr(NULL, NULL);
262 258
    return fsync(fd);
263 259
}
264 260

  
265 261
FileOperations local_ops = {
266 262
    .lstat = local_lstat,
267
    .setuid = local_setuid,
268 263
    .readlink = local_readlink,
269 264
    .close = local_close,
270 265
    .closedir = local_closedir,

Also available in: Unified diff