Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp1199674rdg; Fri, 11 Aug 2023 13:09:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEjPDiOLfaQS/tuFxiN4EEn/vRAWz5yCxq7ETsrN8dZxYntR7dDGZmqQ5EqLAU6nK8+4RO0 X-Received: by 2002:a05:6a20:144b:b0:142:952d:3e61 with SMTP id a11-20020a056a20144b00b00142952d3e61mr3834359pzi.7.1691784566428; Fri, 11 Aug 2023 13:09:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691784566; cv=none; d=google.com; s=arc-20160816; b=TYIKrGBXKLi4Pceh3ouWFT7vWjWEaz7fPBUE/KwdecZaliEtmxXqd6feEMQyNwDSiJ SSBKyj82SlzLLlN6ZpZ079BLA38Xhr8Z++JJieOLxDjRuPcM6cVJh5m/nn5xAFxvF47F gSEvfqrsVcD1JuZGeXmlVxpnu4mbVIJ2ieWWRXdujFeXqNeZsRxtb+8m8+fKCmj2BvdC ixYMujYZZsVGEdSzswpRh2/tWaw82Dtfct+3tA8zira2cWFYIhmjQoBsSiyNBJr90V6j EB4iPvm2bqc815JO9mslBMPRKWLJCt37eeHPx1seA51cvq26TaFz/vEtv4vnq8QGc5wi trTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=UaEIoEPyIbrzcTu0e9mWV6KwjArPvWMZJhSaISbAzwg=; fh=S5GR964GdonALO0CjH4bj5/Q2O7jxL9hddTWZUueXuI=; b=Kl8AMHqE+V7PekTDEbwiPdK//+dGZf1xe0SaAs2isYBwQtUmuWZVxsywr8+hV9LZth KNY9jzmOpr4RYVYp4h+jvTUGymzXAK0OYl6RmbQh6giXuTdVTNgf+T+VqkqtPc1GlY7z koHAaPHTlhdDofNvDDEP/AFdKnEIT/o1lbyXg/Usrwok3lzMnM3YZ2KwIN6tFVKtNbfQ Ai0X1cA7MyPqcjYJnOwOSHDB5vIeF3JxNNf5QpA/Jz4YbW+dGHTjhh/5AqhiBMGbe/ID iuw1l1R+LVTVAa7bN369sNvPW80gdprqbUpyuErizZ2W6t+JMVSbyux2NjVVB5SaKOZ8 NZYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bnF9oyE6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l128-20020a633e86000000b00564a70443b2si3797977pga.358.2023.08.11.13.09.14; Fri, 11 Aug 2023 13:09:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bnF9oyE6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235638AbjHKTFb (ORCPT + 99 others); Fri, 11 Aug 2023 15:05:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233512AbjHKTFa (ORCPT ); Fri, 11 Aug 2023 15:05:30 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C105219A5 for ; Fri, 11 Aug 2023 12:05:29 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5165D63311 for ; Fri, 11 Aug 2023 19:05:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BBC0C433C8; Fri, 11 Aug 2023 19:05:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691780728; bh=Iu/6JyTKZAcWtIyDZiIJjyB72dUpwNi0RC+cpyo64zU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bnF9oyE6bpNSvNl0b4poM+UYLrc6CQNLRSuZlOmg92Ub/+5yzmBgVDItITihXTliP cqkImR+YsyrIa8Iv5IsedovuGIgMTFp7CR9bQ1WJ7ojUmxZYa5hjJ21y5ro6Eo8tiz ZWwttTULYH8YWgFCSTg5AUDwXk+rXkq10TOhFnt9Fj64voegf2MBcaNEf9JQ+40v+p qtVrLeH0XEpUs78SXx8Q5PHFxl8u1qr9tOqPMpaotXXim03Vm01sSPo7RPaYpj6Oyg WZrBG4+BXvPT2Ycm3FzZ3+B+fR8MofRrBTdzMTlv0gGo6UEUQdU7WmKwVugisd0Crr DNz1RanwvSlxw== Received: by pali.im (Postfix) id 557FB677; Fri, 11 Aug 2023 21:05:25 +0200 (CEST) Date: Fri, 11 Aug 2023 21:05:25 +0200 From: Pali =?utf-8?B?Um9ow6Fy?= To: Kees Cook Cc: Kees Cook , Eric Biederman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: binfmt_misc & different PE binaries Message-ID: <20230811190525.hustdrwlgc232dmx@pali> References: <20230706115550.sqyh3k26e2glz2lu@pali> <20230806162346.v7gjoev2nepxlcox@pali> <20230807170852.yefmkcqwum6gdao6@pali> <202308101323.F17474FEB6@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <202308101323.F17474FEB6@keescook> User-Agent: NeoMutt/20180716 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thursday 10 August 2023 13:24:55 Kees Cook wrote: > On Mon, Aug 07, 2023 at 07:08:52PM +0200, Pali Rohár wrote: > > On Monday 07 August 2023 07:45:08 Kees Cook wrote: > > > On August 6, 2023 9:23:46 AM PDT, "Pali Rohár" wrote: > > > >Hello, I would like to remind this email about binfmt_misc for PE. > > > > > > > >On Thursday 06 July 2023 13:55:50 Pali Rohár wrote: > > > >> Hello, > > > >> > > > >> I would like to ask how to properly register binfmt_misc for different > > > >> PE binaries, so kernel could execute the correct loader for them. > > > >> > > > >> I mean, how to register support for Win32 (console/gui) PE binaries and > > > >> also for CLR PE binaries (dotnet). Win32 needs to be executed under wine > > > >> and CLR ideally under dotnet core (or mono). > > > >> > > > >> I have read kernel documentation files admin-guide/binfmt-misc.rst > > > >> and admin-guide/mono.rst. But seems that they are in conflicts as both > > > >> wants to registers its own handler for the same magic: > > > >> > > > >> echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register > > > >> > > > >> echo ':CLR:M::MZ::/usr/bin/mono:' > /proc/sys/fs/binfmt_misc/register > > > >> > > > >> Not mentioning the fact that they register DOS MZ handler, which matches > > > >> not only all PE binaries (including EFI, libraries, other processors), > > > >> but also all kind of other NE/LE/LX binaries and different DOS extenders. > > > >> > > > >> From documentation it looks like that even registering PE binaries is > > > >> impossible by binfmt_misc as PE is detected by checking that indirect > > > >> reference from 0x3C is PE\0\0. And distinguish between Win32 and CLR > > > >> needs to parse PE COM descriptor directory. > > > >> > > > >> Or it is possible to write binfmt_misc pattern match based on indirect > > > >> offset? > > > > > > Normally a single userspace program will be registered and it can do whatever it needs to do to further distinguish the binary and hand it off to the appropriate loader. > > > > Ok, so you are saying that there should be one userspace program which > > distinguish between DOS, CLR and Win32 and then exec the correct > > "runtime" loader? Is there such one? Also it would be nice to mention it > > in the documentation. > > I've not spent much time with it, but I think Wine can be set up to > do this? Hm... I have not figured out how. Quick look into wine source code and it looks like that for dos executables has wine hardcoded spawning of dosbox binary with prepared command line and config file. And for dotnet it executes wine's version of mono which has to be installed separately. Which is not really suitable for CLR/C# applications not dependent on Win32 API and also not very useful for modern dotnet / C# applications which should be run under native Linux dotnet runtime and which mono cannot execute. But I agree that this it is useful for C# applications which use Win32 API (as it is the wine who implements Win32). So I have feeling that there is no ideal solution. I will try to prepare some small deciding program which can act as MZ loader for binfmt_misc and which just exec the correct loader for binary after their MZ and PE headers. > Anyway, I'm happy to apply Documentation patches, if you want to send > changes that would make things more clear. :) Saying something that due to difficulty in parsing of MZ/PE binaries it is not possible distinguish between Win32 and CLR PE application in binfmt_misc module and that current manuals describe how to run all executables with dos header either under wine or mono (not both). I can prepare something. But I'm not sure when was this documentation last time read as it looks like that nobody asked such question. > -Kees > > -- > Kees Cook