From patchwork Mon Jan 30 12:13:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 721463 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vBqGR5Hs2z9sXx for ; Mon, 30 Jan 2017 23:59:11 +1100 (AEDT) Received: from localhost ([::1]:60152 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cYBY5-0005rI-Bk for incoming@patchwork.ozlabs.org; Mon, 30 Jan 2017 07:59:09 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46766) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cYArZ-00010P-Dw for qemu-devel@nongnu.org; Mon, 30 Jan 2017 07:15:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cYArW-0001EU-9e for qemu-devel@nongnu.org; Mon, 30 Jan 2017 07:15:13 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:41021 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cYArW-0001E1-45 for qemu-devel@nongnu.org; Mon, 30 Jan 2017 07:15:10 -0500 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v0UCDpiS027919 for ; Mon, 30 Jan 2017 07:15:09 -0500 Received: from e18.ny.us.ibm.com (e18.ny.us.ibm.com [129.33.205.208]) by mx0b-001b2d01.pphosted.com with ESMTP id 28a1ah1qhv-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 30 Jan 2017 07:15:09 -0500 Received: from localhost by e18.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 30 Jan 2017 07:13:38 -0500 Received: from d01dlp03.pok.ibm.com (9.56.250.168) by e18.ny.us.ibm.com (146.89.104.205) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 30 Jan 2017 07:13:36 -0500 Received: from b01cxnp22036.gho.pok.ibm.com (b01cxnp22036.gho.pok.ibm.com [9.57.198.26]) by d01dlp03.pok.ibm.com (Postfix) with ESMTP id 50E86C90043; Mon, 30 Jan 2017 07:13:17 -0500 (EST) Received: from b01ledav006.gho.pok.ibm.com (b01ledav006.gho.pok.ibm.com [9.57.199.111]) by b01cxnp22036.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v0UCDZdC47382714; Mon, 30 Jan 2017 12:13:35 GMT Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 978B8AC041; Mon, 30 Jan 2017 07:13:35 -0500 (EST) Received: from bahia.lan (unknown [9.164.157.254]) by b01ledav006.gho.pok.ibm.com (Postfix) with ESMTP id 786D8AC03F; Mon, 30 Jan 2017 07:13:34 -0500 (EST) From: Greg Kurz To: qemu-devel@nongnu.org Date: Mon, 30 Jan 2017 13:13:33 +0100 In-Reply-To: <148577817618.10533.9740628265078537215.stgit@bahia.lan> References: <148577817618.10533.9740628265078537215.stgit@bahia.lan> User-Agent: StGit/0.17.1-20-gc0b1b-dirty MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17013012-0044-0000-0000-0000026A585D X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006524; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000201; SDB=6.00814716; UDB=6.00397662; IPR=6.00592157; BA=6.00005099; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00014105; XFM=3.00000011; UTC=2017-01-30 12:13:37 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17013012-0045-0000-0000-00000697580C Message-Id: <148577841334.10533.5674961039110977977.stgit@bahia.lan> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-01-30_08:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1701300123 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PATCH RFC 31/36] 9pfs: local: introduce symlink-attack safe xattr helpers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Aneesh Kumar K.V" , jannh@google.com, Greg Kurz , ppandit@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" There are no "at" variants for xattr syscalls. This patch implement them using a separate process. Signed-off-by: Greg Kurz --- hw/9pfs/9p-xattr.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/9pfs/9p-xattr.h | 11 ++++ 2 files changed, 167 insertions(+) diff --git a/hw/9pfs/9p-xattr.c b/hw/9pfs/9p-xattr.c index 19a2daf02f5c..ea0695f37242 100644 --- a/hw/9pfs/9p-xattr.c +++ b/hw/9pfs/9p-xattr.c @@ -15,7 +15,163 @@ #include "9p.h" #include "fsdev/file-op-9p.h" #include "9p-xattr.h" +#include "9p-util.h" +enum { + XATTRAT_OP_GET = 0, + XATTRAT_OP_LIST, + XATTRAT_OP_SET, + XATTRAT_OP_REMOVE +}; + +struct xattrat_data { + ssize_t ret; + int serrno; + char value[0]; +}; + +static void munmap_preserver_errno(void *addr, size_t length) +{ + int serrno = errno; + munmap(addr, length); + errno = serrno; +} + +static ssize_t do_xattrat_op(int op_type, int dirfd, const char *path, + const char *name, void *value, size_t size, + int flags) +{ + struct xattrat_data *data; + pid_t pid; + ssize_t ret = -1; + int wstatus; + + data = mmap(NULL, sizeof(*data) + size, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (data == MAP_FAILED) { + return -1; + } + data->ret = -1; + + pid = fork(); + if (pid < 0) { + goto err_out; + } else if (pid == 0) { + if (fchdir(dirfd) == 0) { + switch (op_type) { + case XATTRAT_OP_GET: + data->ret = lgetxattr(path, name, data->value, size); + break; + case XATTRAT_OP_LIST: + data->ret = llistxattr(path, data->value, size); + break; + case XATTRAT_OP_SET: + data->ret = lsetxattr(path, name, data->value, size, flags); + break; + case XATTRAT_OP_REMOVE: + data->ret = lremovexattr(path, name); + break; + default: + g_assert_not_reached(); + } + } + data->serrno = errno; + _exit(0); + } + assert(waitpid(pid, &wstatus, 0) == pid && WIFEXITED(wstatus)); + + ret = data->ret; + if (ret < 0) { + errno = data->serrno; + goto err_out; + } + memcpy(value, data->value, data->ret); + +err_out: + munmap_preserver_errno(data, sizeof(*data) + size); + return ret; +} + +ssize_t fgetxattrat(int dirfd, const char *path, const char *name, void *value, + size_t size) +{ + return do_xattrat_op(XATTRAT_OP_GET, dirfd, path, name, value, size, 0); +} + +ssize_t local_getxattr_nofollow(FsContext *ctx, const char *path, + const char *name, void *value, size_t size) +{ + char *dirpath = local_dirname(path); + char *filename = local_basename(path); + int dirfd; + ssize_t ret = -1; + + dirfd = local_opendir_nofollow(ctx, dirpath); + if (dirfd == -1) { + goto out; + } + + ret = fgetxattrat(dirfd, filename, name, value, size); + close_preserve_errno(dirfd); +out: + g_free(dirpath); + g_free(filename); + return ret; +} + +static ssize_t fsetxattrat(int dirfd, const char *path, const char *name, + void *value, size_t size, int flags) +{ + return do_xattrat_op(XATTRAT_OP_SET, dirfd, path, name, value, size, flags); +} + +ssize_t local_setxattr_nofollow(FsContext *ctx, const char *path, + const char *name, void *value, size_t size, + int flags) +{ + char *dirpath = local_dirname(path); + char *filename = local_basename(path); + int dirfd; + ssize_t ret = -1; + + dirfd = local_opendir_nofollow(ctx, dirpath); + if (dirfd == -1) { + goto out; + } + + ret = fsetxattrat(dirfd, filename, name, value, size, flags); + close_preserve_errno(dirfd); +out: + g_free(dirpath); + g_free(filename); + return ret; +} + +static ssize_t fremovexattrat(int dirfd, const char *path, const char *name) +{ + return do_xattrat_op(XATTRAT_OP_GET, dirfd, path, name, NULL, 0, 0); +} + +ssize_t local_removexattr_nofollow(FsContext *ctx, const char *path, + const char *name) +{ + char *dirpath = local_dirname(path); + char *filename = local_basename(path); + int dirfd; + ssize_t ret = -1; + + dirfd = local_opendir_nofollow(ctx, dirpath); + if (dirfd == -1) { + goto out; + } + + ret = fremovexattrat(dirfd, filename, name); + close_preserve_errno(dirfd); +out: + g_free(dirpath); + g_free(filename); + return ret; +} static XattrOperations *get_xattr_operations(XattrOperations **h, const char *name) diff --git a/hw/9pfs/9p-xattr.h b/hw/9pfs/9p-xattr.h index 3f43f5153f3c..d95ccc26a18d 100644 --- a/hw/9pfs/9p-xattr.h +++ b/hw/9pfs/9p-xattr.h @@ -15,6 +15,7 @@ #define QEMU_9P_XATTR_H #include "qemu/xattr.h" +#include "9p-local.h" typedef struct xattr_operations { @@ -29,6 +30,16 @@ typedef struct xattr_operations const char *path, const char *name); } XattrOperations; +ssize_t fgetxattrat(int dirfd, const char *path, const char *name, void *value, + size_t size); + +ssize_t local_getxattr_nofollow(FsContext *ctx, const char *path, + const char *name, void *value, size_t size); +ssize_t local_setxattr_nofollow(FsContext *ctx, const char *path, + const char *name, void *value, size_t size, + int flags); +ssize_t local_removexattr_nofollow(FsContext *ctx, const char *path, + const char *name); extern XattrOperations mapped_user_xattr; extern XattrOperations passthrough_user_xattr;