mirror of https://github.com/xemu-project/xemu.git
9pfs: local: readlink: don't follow symlinks
The local_readlink() callback is vulnerable to symlink attacks because it calls: (1) open(O_NOFOLLOW) which follows symbolic links for all path elements but the rightmost one (2) readlink() which follows symbolic links for all path elements but the rightmost one This patch converts local_readlink() to rely on open_nofollow() to fix (1) and opendir_nofollow(), readlinkat() to fix (2). This partly fixes CVE-2016-9602. Signed-off-by: Greg Kurz <groug@kaod.org> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
parent
ac125d993b
commit
bec1e9546e
|
@ -340,27 +340,35 @@ static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
|
||||||
char *buf, size_t bufsz)
|
char *buf, size_t bufsz)
|
||||||
{
|
{
|
||||||
ssize_t tsize = -1;
|
ssize_t tsize = -1;
|
||||||
char *buffer;
|
|
||||||
char *path = fs_path->data;
|
|
||||||
|
|
||||||
if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
|
if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
|
||||||
(fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
|
(fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
|
||||||
int fd;
|
int fd;
|
||||||
buffer = rpath(fs_ctx, path);
|
|
||||||
fd = open(buffer, O_RDONLY | O_NOFOLLOW);
|
fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
|
||||||
g_free(buffer);
|
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
tsize = read(fd, (void *)buf, bufsz);
|
tsize = read(fd, (void *)buf, bufsz);
|
||||||
} while (tsize == -1 && errno == EINTR);
|
} while (tsize == -1 && errno == EINTR);
|
||||||
close(fd);
|
close_preserve_errno(fd);
|
||||||
} else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
|
} else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
|
||||||
(fs_ctx->export_flags & V9FS_SM_NONE)) {
|
(fs_ctx->export_flags & V9FS_SM_NONE)) {
|
||||||
buffer = rpath(fs_ctx, path);
|
char *dirpath = g_path_get_dirname(fs_path->data);
|
||||||
tsize = readlink(buffer, buf, bufsz);
|
char *name = g_path_get_basename(fs_path->data);
|
||||||
g_free(buffer);
|
int dirfd;
|
||||||
|
|
||||||
|
dirfd = local_opendir_nofollow(fs_ctx, dirpath);
|
||||||
|
if (dirfd == -1) {
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
tsize = readlinkat(dirfd, name, buf, bufsz);
|
||||||
|
close_preserve_errno(dirfd);
|
||||||
|
out:
|
||||||
|
g_free(name);
|
||||||
|
g_free(dirpath);
|
||||||
}
|
}
|
||||||
return tsize;
|
return tsize;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue