From patchwork Wed Apr 29 12:47:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 11516957 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EEAA615E6 for ; Wed, 29 Apr 2020 12:48:25 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C41D72087E for ; Wed, 29 Apr 2020 12:48:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OyNMlIc9" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C41D72087E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:51528 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jTm8W-0002RR-TG for patchwork-qemu-devel@patchwork.kernel.org; Wed, 29 Apr 2020 08:48:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58378) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jTm7q-0001zE-1T for qemu-devel@nongnu.org; Wed, 29 Apr 2020 08:47:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jTm7p-00060r-3P for qemu-devel@nongnu.org; Wed, 29 Apr 2020 08:47:41 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:54945 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jTm7o-0005og-Kb for qemu-devel@nongnu.org; Wed, 29 Apr 2020 08:47:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588164458; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2hyVXtBIZ5nsj6xHXRkKQNxCFdNeKoeT3XonE68maGE=; b=OyNMlIc9TyajdFGkLfWRNjhvCfVw9NJfJwoaNx3/uaT1sY8fUKjv5SR8bfaJEfJZXPfc6E Jka4+G5hsYsWVgkWshu8DTL8/WwyARas1CmTiTDhuE06ktgvPNggxZRNfTsNVktkdmIW9F 7quesgv3evss1Za8OBhXdYYbgpVCXT0= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-39-ue5Bss45NZCVNfWN1Qwg-w-1; Wed, 29 Apr 2020 08:47:37 -0400 X-MC-Unique: ue5Bss45NZCVNfWN1Qwg-w-1 Received: by mail-wr1-f69.google.com with SMTP id f4so1668838wrp.14 for ; Wed, 29 Apr 2020 05:47:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ECN+P6CCFmZwUjw2WoWwRMDTufsKlIl5f+1ZGHAXC1c=; b=ryykIoeXkMxi4skW6M217FcS9YUqzzGkIPidUulYMimTrwXcrBVoHbvlZpprPg2oht kSFWnSU/kUgKLIBn2ebSIJUsnI3hgmcV/DF3h+vHKvfkNz3T2Mm8UpjgYMAEypIcwD4a RwuvK02RKJzUwyIYItE5U8llbm8vGtl57V00qBnSFB0YdNQpdHCd8pXbVDS1DcoiJo1Q lkhauPBM4Rp6TCWs8PabmGgXwkvyzFGVFqfmqAYRvO8o9MiiNohwN4zeyw21PJ92tMpx pa2IAAPK1eA75PYNVlydFb1na+JH3a2TnH9C8jxEYFDeyy1rKwrY/MrxRykSlomOgyTi drlA== X-Gm-Message-State: AGi0PuYqfm6rZeQ88KW603nvvpa2Tvebv09OCddNPP7nj9oxxvpvQlKs tGGMq84dUCqT6X9rzj6csMec3dqmxv63aZWWeUKaztLXc8NAnG0G4y4p5z9XuzvvaggLZyVzzyT /OuyZM2fpolRDc74= X-Received: by 2002:a05:600c:218e:: with SMTP id e14mr3299558wme.140.1588164455669; Wed, 29 Apr 2020 05:47:35 -0700 (PDT) X-Google-Smtp-Source: APiQypI6Dn8fpfB5e+UgjD9jEZBgJA3iIrSB0pWcv/lF83HT1FBpL6q4FaOEcROAwE69rdxSE70fRw== X-Received: by 2002:a05:600c:218e:: with SMTP id e14mr3299542wme.140.1588164455412; Wed, 29 Apr 2020 05:47:35 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (catv-212-96-48-140.catv.broadband.hu. [212.96.48.140]) by smtp.gmail.com with ESMTPSA id o6sm21813078wrw.63.2020.04.29.05.47.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 05:47:34 -0700 (PDT) From: Miklos Szeredi To: qemu-devel@nongnu.org Subject: [PATCH] virtiofsd: jail lo->proc_self_fd Date: Wed, 29 Apr 2020 14:47:33 +0200 Message-Id: <20200429124733.22488-1-mszeredi@redhat.com> X-Mailer: git-send-email 2.21.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass client-ip=207.211.31.120; envelope-from=mszeredi@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/29 01:18:10 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: virtio-fs@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" While it's not possible to escape the proc filesystem through lo->proc_self_fd, it is possible to escape to the root of the proc filesystem itself through "../..". Use a temporary mount for opening lo->proc_self_fd, that has it's root at /proc/self/fd/, preventing access to the ancestor directories. Signed-off-by: Miklos Szeredi Reviewed-by: Stefan Hajnoczi --- tools/virtiofsd/passthrough_ll.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c index 4c35c95b256c..bc9c44c760f4 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -2536,6 +2536,8 @@ static void print_capabilities(void) static void setup_namespaces(struct lo_data *lo, struct fuse_session *se) { pid_t child; + char template[] = "virtiofsd-XXXXXX"; + char *tmpdir; /* * Create a new pid namespace for *child* processes. We'll have to @@ -2597,12 +2599,33 @@ static void setup_namespaces(struct lo_data *lo, struct fuse_session *se) exit(1); } + tmpdir = mkdtemp(template); + if (!tmpdir) { + fuse_log(FUSE_LOG_ERR, "tmpdir(%s): %m\n", template); + exit(1); + } + + if (mount("/proc/self/fd", tmpdir, NULL, MS_BIND, NULL) < 0) { + fuse_log(FUSE_LOG_ERR, "mount(/proc/self/fd, %s, MS_BIND): %m\n", + tmpdir); + exit(1); + } + /* Now we can get our /proc/self/fd directory file descriptor */ - lo->proc_self_fd = open("/proc/self/fd", O_PATH); + lo->proc_self_fd = open(tmpdir, O_PATH); if (lo->proc_self_fd == -1) { - fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n"); + fuse_log(FUSE_LOG_ERR, "open(%s, O_PATH): %m\n", tmpdir); exit(1); } + + if (umount2(tmpdir, MNT_DETACH) < 0) { + fuse_log(FUSE_LOG_ERR, "umount2(%s, MNT_DETACH): %m\n", tmpdir); + exit(1); + } + + if (rmdir(tmpdir) < 0) { + fuse_log(FUSE_LOG_ERR, "rmdir(%s): %m\n", tmpdir); + } } /*