From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 697E2803F6 for ; Mon, 16 Nov 2026 06:14:26 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AG6DDNW020478; Mon, 16 Nov 2026 09:13:14 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AG6DDNW020478 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="d6gQfJeZ" Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AG6D89x020459 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Mon, 16 Nov 2026 09:13:08 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AG6D89x020459 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4a80bd45e1cso14001895e9.3 for ; Sun, 15 Nov 2026 22:13:08 -0800 (PST) X-Gm-Message-State: APuU4Z8OyoPDlqUhSwjvKlGik8spJARgbduyb+xO8NDVQp2wg0Ec/9au hADkxV0GSmUm+lRiOjwmlwhIJ8kl2CTbhCxkLBa+HoBWUm3edxSKkeObC6mICrogOnPqfrGraXd DXcTYcu0NmoXtrtyEkeVhzYir+IIPCrsjG24QcvJ0SgtGneqqwJpUrw== X-Gm-Gg: AYBFou3qUwGxk6HrDHjTzgndZS0mPjUTLfByB9u2hSCJbQNemAOX1t/nm0qJRnAZFS4 nvlk5H/gPlA2tSXj7FGi8ToBDiNa9pg4U4yxiGP5wLWO+FG4L9uNYlWjaZxQjFjmUj9BREeb3cf wfySvviDBRl0zNAStF4Yz7B/Wzs02uuZABNu3dEbhaMj23SHA05VFZ0NENX9OojyImBoqxgX4jd FSgDl/+eajyL1hH7PIxyq2nn7MneBFRoppgMHOH/J2VukHrQtnh8xPD4jmjC1i8s5H1DL0m7Wc7 DQuxgMt/CoQIgxZBQl18K94xBZ9uOHWAlQNl3kbfE7eQy88lK1CXCUeWeL16jQcfT7DYzpIuecO VZSYjarOEEd46tlVot5Zl5o3abz7V X-Received: by 2002:a05:600c:608c:b0:4bb:680f:9a3 with SMTP id 5b1f17b1804b1-4bb680f0a64mr104992715e9.2.1794809587928; Sun, 15 Nov 2026 22:13:07 -0800 (PST) X-Received: by 2002:a05:600c:608c:b0:4bb:680f:9a3 with SMTP id 5b1f17b1804b1-4bb680f0a64mr104992365e9.2.1794809587488; Sun, 15 Nov 2026 22:13:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794809587; cv=none; d=google.com; s=arc-20260327; b=nDiOIeub2XSmxCUZ3S9nbhMhjrm6Czi23nPJJhoTXgNXhZaNnh4VjWuLCIwG8qfsbx PYpXYFCUFzh1p2aUnNvm3wy8l3cXE+V4GzwsxKyMOa8RFYJ6aZERJHps320Y1h+dlrBR hNOtb4zNwbUJ+kuaOEjs98SJtIsv/iKJcPnkhK8oRV9jHsUEH4KwNQGA8Yc2stzHKcbB b8rPtsf38A4vgjjQQhUubaOGh+nAVKPtPem1UEBpEld8dcGbyTmvoxPj/fuvBhtdn60c vQP+qvPStf3vMVUZdVhBtQ25ejeCa+Vx1xo4PPhBD2WbiMC4n0x4bSVZpfVqK+GPkax3 kc+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=Cae/80xPqrspHwkxI/HpsTvLFgC0a4Y84c9Jhvw6FJs=; fh=wpoNUURlquLpJK/14Kny0oMIYkY7mYwjJ/Ils9UT2kk=; b=nIHVxmfia7Jw9wjMxMnSfEU5r9n2daSYBEqTeqrx5b3JKnI6xy2wjab28TXZwjtM+Y ZUL4d2/35nRSk7p8mS7kVKMFuoa/vkvsXQTHhAtDoBii0xGsfP81kXsxjFSqaHl5Z3zu k/J1X6Njss2HmcU0Y+Lv6hXcXn1EQGUhVxmn7MlQYdbXhGInzdRjqD2vq/C5WDGU4UCi K1CUhpf73W2e5AT6WWM6zlBZfbvZvQaOPspY9iANvO8gFhY8hWTgqNvtqrmyvTPbFW4v rb/1Zb9tfKKJ3sQ8iOQtuFjwApnssxnpPgHbrjrHxrHd9JI4+NGR7kmF6WJz2dB08coM qbwg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=d6gQfJeZ; spf=pass (google.com: domain of mirian.shilakadze@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=mirian.shilakadze@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id ffacd0b85a97d-49c64b0a2e8si19451455f8f.396.2026.11.15.22.13.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Nov 2026 22:13:07 -0800 (PST) Received-SPF: pass (google.com: domain of mirian.shilakadze@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=d6gQfJeZ; spf=pass (google.com: domain of mirian.shilakadze@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=mirian.shilakadze@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-ID:Date:Subject:From: Content-Type; bh=Cae/80xPqrspHwkxI/HpsTvLFgC0a4Y84c9Jhvw6FJs=; b=d6gQfJeZTLG0 c+jP4Dm6epsH+UvHzDGdTPdQYaZe8RgAYKBEVTsU41HVCNgHkHS62WomZmLxE1ab+Q4Tcoadypq8z RggM1y7dxfU2Jyl5+gBDUzAxJNw3dALHilLjd5CxyrJeUbX2/Qh7PuoGkq0d8BZ+tD/x4E4Bzltk+ T4xM/DTeb/Svl5ZZ97p8x63Ol6s6nEs2AOpZ8TiIHYE4F9mTvZ5tdDugJdCDhcLBefJcNlm6RDC1L XgUBwAScazHPkwFI6gnqWPbYSSEBWqMifMUG8j7azRzZff4v1fQcR+xw/AI2yCXS4OuXeQ5odZe92 yE4V/qD/e6t5v3FhLmdlHg==; Received: from ch-demo-asa.virtuozzo.com ([130.117.225.8] helo=localhost.localdomain) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1xSptU-00Cj8x-1n; Mon, 16 Nov 2026 07:12:59 +0100 From: Mirian Shilakadze To: khorenko@virtuozzo.com, ptikhomirov@virtuozzo.com Date: Mon, 16 Nov 2026 10:12:52 +0400 Message-ID: <20261116061256.54472-2-mirian.shilakadze@virtuozzo.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261116061256.54472-1-mirian.shilakadze@virtuozzo.com> References: <20261116061256.54472-1-mirian.shilakadze@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH vz10 v2 1/3] fs: let the protection flags of a mount be locked from userspace X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org A container can strip ro, nosuid, nodev or noexec from a bind mount the host gave it with "mount -o remount,rw,bind" and then write host data. Nothing raises the MNT_LOCK_* bits that forbid this on such a mount, and userspace cannot set them. Give each of them a mount_setattr() attribute, MOUNT_ATTR_VE_LOCK_RDONLY, _NOSUID, _NODEV and _NOEXEC, with MOUNT_ATTR_VE_LOCK for all four. A lock stands only where its protection is set and is never dropped, attr_clr refuses it. Only CAP_SYS_ADMIN in the initial user namespace may lock, on one mount at a time. Locks can now be raised on an attached mount, so remount tests them again under the lock that guards the change. Feature: ve: ve generic structures https://virtuozzo.atlassian.net/browse/VSTOR-155305 Signed-off-by: Mirian Shilakadze --- fs/namespace.c | 58 ++++++++++++++++++++++++++++++++++---- include/uapi/linux/mount.h | 15 ++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index 75cabf7763ef..fa3c1851b7da 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -100,6 +100,7 @@ struct mount_kattr { unsigned int propagation; unsigned int lookup_flags; bool recurse; + bool lock_flags; struct user_namespace *mnt_userns; struct mnt_idmap *mnt_idmap; }; @@ -3018,8 +3019,9 @@ SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, fl /* * Don't allow locked mount flags to be cleared. * - * No locks need to be held here while testing the various MNT_LOCK - * flags because those flags can never be cleared once they are set. + * mount_setattr() can lock an attached mount, under the mount hash and + * s_umount held for read, so test again under the hash or s_umount held + * for write before acting on the result. */ static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags) { @@ -3124,7 +3126,9 @@ static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags) */ down_read(&sb->s_umount); lock_mount_hash(); - ret = change_mount_ro_state(mnt, mnt_flags); + ret = -EPERM; + if (can_change_locked_flags(mnt, mnt_flags)) + ret = change_mount_ro_state(mnt, mnt_flags); if (ret == 0) set_mount_attributes(mnt, mnt_flags); unlock_mount_hash(); @@ -3479,7 +3483,8 @@ static int do_remount(struct path *path, int ms_flags, int sb_flags, if (!err) { down_write(&sb->s_umount); err = -EPERM; - if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) { + if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN) && + can_change_locked_flags(mnt, mnt_flags)) { err = reconfigure_super(fc); if (!err) { lock_mount_hash(); @@ -4543,7 +4548,10 @@ SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME | \ MOUNT_ATTR_NOSYMFOLLOW) -#define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP) +#define MOUNT_SETATTR_LOCK_FLAGS MOUNT_ATTR_VE_LOCK + +#define MOUNT_SETATTR_VALID_FLAGS \ + (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP | MOUNT_SETATTR_LOCK_FLAGS) #define MOUNT_SETATTR_PROPAGATION_FLAGS \ (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED) @@ -4564,6 +4572,14 @@ static unsigned int attr_flags_to_mnt_flags(u64 attr_flags) mnt_flags |= MNT_NODIRATIME; if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW) mnt_flags |= MNT_NOSYMFOLLOW; + if (attr_flags & MOUNT_ATTR_VE_LOCK_RDONLY) + mnt_flags |= MNT_LOCK_READONLY; + if (attr_flags & MOUNT_ATTR_VE_LOCK_NOSUID) + mnt_flags |= MNT_LOCK_NOSUID; + if (attr_flags & MOUNT_ATTR_VE_LOCK_NODEV) + mnt_flags |= MNT_LOCK_NODEV; + if (attr_flags & MOUNT_ATTR_VE_LOCK_NOEXEC) + mnt_flags |= MNT_LOCK_NOEXEC; return mnt_flags; } @@ -4915,6 +4931,15 @@ static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt) flags &= ~kattr->attr_clr; /* flags to raise */ flags |= kattr->attr_set; + /* a protection lock only stands where the protection is set */ + if (!(flags & MNT_READONLY)) + flags &= ~MNT_LOCK_READONLY; + if (!(flags & MNT_NOSUID)) + flags &= ~MNT_LOCK_NOSUID; + if (!(flags & MNT_NODEV)) + flags &= ~MNT_LOCK_NODEV; + if (!(flags & MNT_NOEXEC)) + flags &= ~MNT_LOCK_NOEXEC; return flags; } @@ -5083,6 +5108,10 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr) kattr->mnt_idmap = mnt_idmap; } + /* do_remount() tests the locks and changes the flags under s_umount */ + if (kattr->lock_flags) + down_read(&mnt->mnt.mnt_sb->s_umount); + if (kattr->propagation) { /* * Only take namespace_lock() if we're actually changing @@ -5093,7 +5122,7 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr) err = invent_group_ids(mnt, kattr->recurse); if (err) { namespace_unlock(); - return err; + goto out_lock; } } } @@ -5137,6 +5166,10 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr) namespace_unlock(); } +out_lock: + if (kattr->lock_flags) + up_read(&mnt->mnt.mnt_sb->s_umount); + return err; } @@ -5230,6 +5263,19 @@ static int build_mount_kattr(const struct mount_attr *attr, size_t usize, if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS) return -EINVAL; + /* + * Only the host may lock, one mount per call. Like the ones + * lock_mnt_tree() raises, a protection lock is never dropped. + */ + kattr->lock_flags = (attr->attr_set | attr->attr_clr) & + MOUNT_SETATTR_LOCK_FLAGS; + if (kattr->lock_flags && !capable(CAP_SYS_ADMIN)) + return -EPERM; + if (attr->attr_clr & MOUNT_ATTR_VE_LOCK) + return -EINVAL; + if (kattr->lock_flags && kattr->recurse) + return -EINVAL; + kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set); kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr); diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h index 225bc366ffcb..8c6a5e643b9c 100644 --- a/include/uapi/linux/mount.h +++ b/include/uapi/linux/mount.h @@ -125,6 +125,21 @@ enum fsconfig_command { #define MOUNT_ATTR_IDMAP 0x00100000 /* Idmap mount to @userns_fd in struct mount_attr. */ #define MOUNT_ATTR_NOSYMFOLLOW 0x00200000 /* Do not follow symlinks */ +/* + * Virtuozzo attributes take bits from 63 down, clear of the upstream ones. + * + * Lock a protection so that whoever gets the mount cannot clear it. Only the + * host locks, one mount per mount_setattr() call. A lock holds only where its + * protection is set and is never dropped. A bind copies the locks. + */ +#define MOUNT_ATTR_VE_LOCK_RDONLY 0x8000000000000000ULL +#define MOUNT_ATTR_VE_LOCK_NOSUID 0x4000000000000000ULL +#define MOUNT_ATTR_VE_LOCK_NODEV 0x2000000000000000ULL +#define MOUNT_ATTR_VE_LOCK_NOEXEC 0x1000000000000000ULL +#define MOUNT_ATTR_VE_LOCK \ + (MOUNT_ATTR_VE_LOCK_RDONLY | MOUNT_ATTR_VE_LOCK_NOSUID | \ + MOUNT_ATTR_VE_LOCK_NODEV | MOUNT_ATTR_VE_LOCK_NOEXEC) + /* * mount_setattr() */ -- 2.43.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel