From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="LAFpHu/z" Received: from smtp-relay-canonical-0.canonical.com (smtp-relay-canonical-0.canonical.com [185.125.188.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72CCE18C for ; Tue, 5 Dec 2023 10:34:44 -0800 (PST) Received: from [192.168.192.84] (unknown [50.39.103.33]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-0.canonical.com (Postfix) with ESMTPSA id BDF4D3F16E; Tue, 5 Dec 2023 18:34:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1701801282; bh=ZO9sPX/fnz5Ahak0iB7eNQ+MezI1C4eNsv6ghCWx330=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=LAFpHu/z8cLCYZjfAyGUD1wWmCOnNan/Tg5gJmWfmRBPwGiioXXeFfHKpZXl7Al+q dhB+Gs0kqQJ5/N51HAZtvZyAuxbV2fYVc+AVtIQ6452u2ZNxfQj11g7wtDo4/JW+Lm BN+g5ZIKJgUmy7xzUhvAmWZ7/LOGOLuwgTIn61dgXbiIrkzmGD6MMSrr/BaHvn5k4Z tXz/GBWkbjLzjqnmvfg2d0hC6fcuupghwTlQkunHwf4fancT/i3m/a2mgfHckIzMHw yjCBucw5YlWPT28+CObGG3acymwty3OmyGPFxLYcWIj5DrwxL44X1rayEvzU0ICloW TpXUq/YwSKQWw== Message-ID: <862e8daa-e586-4627-8698-0416a4eba155@canonical.com> Date: Tue, 5 Dec 2023 10:34:33 -0800 Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Apparmor move_mount mediation breaks mount tool in containers Content-Language: en-US To: Christian Brauner , Linux regressions mailing list Cc: =?UTF-8?Q?St=C3=A9phane_Graber?= , Sasha Levin , Aleksa Sarai , Alexander Mihalicyn , "paul@paul-moore.com" , James Morris , Serge Hallyn , linux-security-module@vger.kernel.org, Linus Torvalds References: <582eb2e9-ce80-4f96-a4bc-bef1a508e0ab@canonical.com> <68c166b8-5b4d-4612-8042-1dee3334385b@leemhuis.info> <20231205-goldwaage-faszination-97b57cbc24b9@brauner> From: John Johansen Autocrypt: addr=john.johansen@canonical.com; keydata= xsFNBE5mrPoBEADAk19PsgVgBKkImmR2isPQ6o7KJhTTKjJdwVbkWSnNn+o6Up5knKP1f49E BQlceWg1yp/NwbR8ad+eSEO/uma/K+PqWvBptKC9SWD97FG4uB4/caomLEU97sLQMtnvGWdx rxVRGM4anzWYMgzz5TZmIiVTZ43Ou5VpaS1Vz1ZSxP3h/xKNZr/TcW5WQai8u3PWVnbkjhSZ PHv1BghN69qxEPomrJBm1gmtx3ZiVmFXluwTmTgJOkpFol7nbJ0ilnYHrA7SX3CtR1upeUpM a/WIanVO96WdTjHHIa43fbhmQube4txS3FcQLOJVqQsx6lE9B7qAppm9hQ10qPWwdfPy/+0W 6AWtNu5ASiGVCInWzl2HBqYd/Zll93zUq+NIoCn8sDAM9iH+wtaGDcJywIGIn+edKNtK72AM gChTg/j1ZoWH6ZeWPjuUfubVzZto1FMoGJ/SF4MmdQG1iQNtf4sFZbEgXuy9cGi2bomF0zvy BJSANpxlKNBDYKzN6Kz09HUAkjlFMNgomL/cjqgABtAx59L+dVIZfaF281pIcUZzwvh5+JoG eOW5uBSMbE7L38nszooykIJ5XrAchkJxNfz7k+FnQeKEkNzEd2LWc3QF4BQZYRT6PHHga3Rg ykW5+1wTMqJILdmtaPbXrF3FvnV0LRPcv4xKx7B3fGm7ygdoowARAQABzStKb2huIEpvaGFu c2VuIDxqb2huLmpvaGFuc2VuQGNhbm9uaWNhbC5jb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fZ2ME+0qKpbQnr1oizLHuJX/Telo8KMmHter28DPJ03 lT9Q Organization: Canonical In-Reply-To: <20231205-goldwaage-faszination-97b57cbc24b9@brauner> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/5/23 09:08, Christian Brauner wrote: > On Tue, Dec 05, 2023 at 09:45:35AM +0100, Linux regression tracking (Thorsten Leemhuis) wrote: >> [CCing Linus] >> >> Hi, top-posting for once, to make this easily accessible to everyone. >> >> Stéphane, many thx for your insights. >> >> I'm CCing Linus on this, as I guess he wants to be aware of this. >> >> Normally I try to sum up things somewhat when doing so, but this here >> likely won't end well. So all I'm saying is: commit 157a3537d6bc28 >> ("apparmor: Fix regression in mount mediation") [v6.7-rc1] that was also >> included in v6.6.3 broke containers in some setups. John described that >> commit with the words "it is a far from good solution. It is a stop >> gap." and later mentioned that "a CVE is coming for this and that having >> this unmediated in the tree isn't good either." >> >> For more details please check out the thread that on lore started with >> this message: >> >> https://lore.kernel.org/all/CA+enf=u0UmgjKrd98EYkxFu7FYV8dR1SBYJn_1b0Naq=3twbbQ@mail.gmail.com/ >> >> Ciao, Thorsten (wearing his 'the Linux kernel's regression tracker' hat) >> -- >> Everything you wanna know about Linux kernel regression tracking: >> https://linux-regtracking.leemhuis.info/about/#tldr >> If I did something stupid, please tell me, as explained on that page. >> >> On 05.12.23 07:57, Stéphane Graber wrote: >>> On Mon, Dec 4, 2023 at 9:20 AM Linux regression tracking (Thorsten >>> Leemhuis) wrote: >>>> >>>> On 04.12.23 14:14, John Johansen wrote: >>>>> On 12/3/23 17:34, Stéphane Graber wrote: >>>> >>>> Side note: Stéphane, thx for CCing the regressions list. >>>> >>>>>> On Sun, Dec 3, 2023 at 8:21 PM John Johansen >>>>>> wrote: >>>>>>> On 12/2/23 17:20, Stéphane Graber wrote: >>>>>>>> >>>>>>>> Upstream commit 157a3537d6bc28ceb9a11fc8cb67f2152d860146 which just >>>>>>>> landed in 6.6.3 stable as 96af45154a0be30485ad07f70f852b1456cb13d7 is >>>>>>>> blocking new mounts for all LXC, LXD and Incus users (at least) on >>>>>>>> distributions using the newer version of util-linux. >>>>>>>> >>>>>>>> That's because for a simple mount like "mount -t tmpfs tmpfs /tmp", >>>>>>>> the new mount command now performs: >>>>>>>> ``` >>>>>>>> fsconfig(3, FSCONFIG_SET_STRING, "source", "tmpfs", 0) = 0 >>>>>>>> fsconfig(3, FSCONFIG_CMD_CREATE, NULL, NULL, 0) = 0 >>>>>>>> fsmount(3, FSMOUNT_CLOEXEC, 0) = 4 >>>>>>>> statx(4, "", AT_STATX_SYNC_AS_STAT|AT_EMPTY_PATH, STATX_MNT_ID, >>>>>>>> {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, >>>>>>>> stx_attributes=STATX_ATTR_MOUNT_ROOT, stx_mode=S_IFDIR|S_ISVTX|0777, >>>>>>>> stx_size=40, ...}) = 0 >>>>>>>> move_mount(4, "", AT_FDCWD, "/tmp", MOVE_MOUNT_F_EMPTY_PATH) = 0 >>>>>>>> ``` >>>>>>>> >>>>>>>> That last call to "move_mount" is incorrectly interpreted by AppArmor >>>>>>>> as an attempt to move-mount "/" to "/mnt" rather than as a new mount >>>>>>>> being created, this therefore results in: >>>>>>>> ``` >>>>>>>> Dec 03 01:05:03 kernel-test kernel: audit: type=1400 >>>>>>>> audit(1701565503.599:34): apparmor="DENIED" operation="mount" >>>>>>>> class="mount" info="failed perms check" error=-13 >>>>>>>> profile="incus-a_" name="/tmp/" pid=2190 comm="mount" >>>>>>>> srcname="/" flags="rw, move" >>>>>>>> ``` > > move_mount() doesn't create a new mount it just moves an already > existing mount around in the tree. Either that mount is moved > (fsmount(), open_tree(OPEN_TREE_CLONE)) from it's source location or it > was a detached mount to begin with. > yes, all to aware of this > The new mount api splits mounting across multiple system calls. And the > state it keeps for itself is gone once you create a mount via fsmount(). yep > Because then the fs_context will be destroyed and you're only dealing > with a superblock and mount(s) for it. > yep > You shouldn't care about move_mount() moving a detached mount because > really it is not a move from some other filesystem location. The > creation of that mount will have already happened in > open_tree(OPEN_TREE_CLONE) or in fsmount(). So that ship has sailed and > move_mount() is the wrong place to do this. > yes and no. What we are specifically dong is mediating existing attached mounts as always and blocking move_mount from attaching the detached mount via move_mount. Which is very much something move_mount mediation should be able to do. It works as a stop gap for the new mount api bypassing the LSM at a very course level. This is why it requires a very general mount rule for apparmor to allow move_mount of the detached mount. A conditional that improves control around this is coming, but we can't make move_mount() mediation provide the full set of apparmor old mount mediation. Providing something close to what we were doing before is going to require new hooks, and likely specialized states tailored to the new mount api. Until that does happen it does mean a reduction in what apparmor policy can mediate. Today instead of being able to specify details about the mount you need a generic mount rule that just allows the application to do pretty much anything with mounts, soon you will be able to have an addition conditional that allows better control of move mount, and better logging of what is going on. > If the only thing that would be broken here is that you want to block > moving mounts from a given source location then you need to pass down > that this is an actual move to the LSM hook. Because I'm really not keen > on giving LSMs access to is_anon_ns() or similar helpers. They're just > too easy to misuse. > indeed the LSM hook here needs to have some more info passed in. >>> Overall, this still feels very rushed and broken to me. I understand >>> that being able to trivially bypass AppArmor mount rules isn't great, >>> but shipping code that makes the vast majority of said rules useless >>> doesn't really feel like such an improvement. It's effectively turning >>> what was a reasonably flexible policy engine around mount calls, into >>> a binary switch to either allow everything or block everything. > > For a 1:1 mapping of AppArmor LSM rules from the old to the new mount > api I expect that one will have to effectively redo everything > internally and keep state across multiple system calls. IMO, this is > really not the way to go. Instead it's probably wise to have a new mount > mediation policy for the new mount api with different semantics. Because > I have my doubts that a 1:1 mapping will even work and won't just cause > you CVEs. yes the mount policy will have to be extended to support the new api. Getting that right will take time.