Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp2865985pxx; Sun, 1 Nov 2020 13:05:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJzCgLss1XX/s3tgwQDK+H6Bc9Rj4Lw5qR7OE9F37nB3PcCtjxvLjQzzYG7N56zW7h4UnIjE X-Received: by 2002:a05:6402:1004:: with SMTP id c4mr13617000edu.149.1604264706117; Sun, 01 Nov 2020 13:05:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604264706; cv=none; d=google.com; s=arc-20160816; b=0lU7+bX7ruk1IYSrXdw92BtqDg+sRHfXdJZ1mqVkZ8s4v+yx4XbDxEPouExieh8QxY 5xPXinG40pxbDujQunfrdjUO7XCBs1x7WBlptdeUJwy7i46vzjwXqtuoYYaZm13+6TZb YqqQU/WXi/UkH9hTk3dpfhAoYcyRXUL51njfEhmsCJiRwjaRmAQokFq0uhZ5sXJZYr+T QPR5SZH/UnRWZaA8EbJQv85C3suvn0st6ybZgplTN1s5zaiavqZ+OYminC8i8gypI8Ub KAQMo3X/Oix30Aq5CV75+bxQ+/Ef8HQUuECW+7hLOmGOkbuz+Zh+i0chuUtcEG79Gsi1 WRWg== 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; bh=gvA57ZQevBKCWy5PGAh1YY2LoBkKCBzlaQf+IK0lb94=; b=yfJAy/bwZVao2T6DeXwvwktpnILda5GYXMsbkq9AnECk8iL45JPnIGHy9TP/JlVLIZ gSPbzocAxrjMw+9HM78soXfim/sjaexaLQFn3FDvhV9pT53BLBisyv4FTH5zDc8+O2ED EfE/fOyMOUN8Z5CAIjsI0jpO/BeEf/MCBgymGmjG4fVbNvRwYArbyhnxurR7o3KIMYuo 7J0LC73VSwE1tciGfuE2wO4RU/8JOrKHgJ/WGxPqqljT3DUlM0FMcIGJFD9ZlUqsVPEn TWTAFtq7NgGAUrWTi/QLHqAb0KT4HhgYmBNldbZB8KjxJD8z6Sq28hjT4ZrN58JYaqrG fQNg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e4si8366001ejh.230.2020.11.01.13.04.40; Sun, 01 Nov 2020 13:05:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727052AbgKAVBE (ORCPT + 99 others); Sun, 1 Nov 2020 16:01:04 -0500 Received: from brightrain.aerifal.cx ([216.12.86.13]:40428 "EHLO brightrain.aerifal.cx" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727016AbgKAVBE (ORCPT ); Sun, 1 Nov 2020 16:01:04 -0500 Date: Sun, 1 Nov 2020 16:01:03 -0500 From: Rich Felker To: Jessica Clarke Cc: Andy Lutomirski , Florian Weimer , linux-x86_64@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , X86 ML , "H. Peter Anvin" , LKML Subject: Re: [PATCH v2] x86: Fix x32 System V message queue syscalls Message-ID: <20201101210102.GO534@brightrain.aerifal.cx> References: <1156938F-A9A3-4EE9-B059-2294A0B9FBFE@jrtc27.com> <20201012134444.1905-1-jrtc27@jrtc27.com> <20201101012202.GM534@brightrain.aerifal.cx> <7842A462-0ADB-4EE3-B4CB-AE6DCD70CE1C@jrtc27.com> <20201101015013.GN534@brightrain.aerifal.cx> <04832096-ED7F-4754-993D-F578D4A90843@jrtc27.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Nov 01, 2020 at 06:27:10PM +0000, Jessica Clarke wrote: > On 1 Nov 2020, at 18:15, Jessica Clarke wrote: > > > > On 1 Nov 2020, at 18:07, Andy Lutomirski wrote: > >> > >> On Sat, Oct 31, 2020 at 6:50 PM Rich Felker wrote: > >>> > >>> On Sun, Nov 01, 2020 at 01:27:35AM +0000, Jessica Clarke wrote: > >>>> On 1 Nov 2020, at 01:22, Rich Felker wrote: > >>>>> On Sat, Oct 31, 2020 at 04:30:44PM -0700, Andy Lutomirski wrote: > >>>>>> cc: some libc folks > >>>>>> > >>>>>> On Mon, Oct 12, 2020 at 6:45 AM Jessica Clarke wrote: > >>>>>>> > >>>>>>> POSIX specifies that the first field of the supplied msgp, namely mtype, > >>>>>>> is a long, not a __kernel_long_t, and it's a user-defined struct due to > >>>>>>> the variable-length mtext field so we can't even bend the spec and make > >>>>>>> it a __kernel_long_t even if we wanted to. Thus we must use the compat > >>>>>>> syscalls on x32 to avoid buffer overreads and overflows in msgsnd and > >>>>>>> msgrcv respectively. > >>>>>> > >>>>>> This is a mess. > >>>>>> > >>>>>> include/uapi/linux/msg.h has: > >>>>>> > >>>>>> /* message buffer for msgsnd and msgrcv calls */ > >>>>>> struct msgbuf { > >>>>>> __kernel_long_t mtype; /* type of message */ > >>>>>> char mtext[1]; /* message text */ > >>>>>> }; > >>>>>> > >>>>>> Your test has: > >>>>>> > >>>>>> struct msg_long { > >>>>>> long mtype; > >>>>>> char mtext[8]; > >>>>>> }; > >>>>>> > >>>>>> struct msg_long_ext { > >>>>>> struct msg_long msg_long; > >>>>>> char mext[4]; > >>>>>> }; > >>>>>> > >>>>>> and I'm unclear as to exactly what you're trying to do there with the > >>>>>> "mext" part. > >>>>>> > >>>>>> POSIX says: > >>>>>> > >>>>>> The application shall ensure that the argument msgp points to a user- > >>>>>> defined buffer that contains first a field of type long specifying the > >>>>>> type of the message, and then a data portion that holds the data bytes > >>>>>> of the message. The structure below is an example of what this user-de‐ > >>>>>> fined buffer might look like: > >>>>>> > >>>>>> struct mymsg { > >>>>>> long mtype; /* Message type. */ > >>>>>> char mtext[1]; /* Message text. */ > >>>>>> } > >>>>>> > >>>>>> NTP has this delightful piece of code: > >>>>>> > >>>>>> 44 typedef union { > >>>>>> 45 struct msgbuf msgp; > >>>>>> 46 struct { > >>>>>> 47 long mtype; > >>>>>> 48 int code; > >>>>>> 49 struct timeval tv; > >>>>>> 50 } msgb; > >>>>>> 51 } MsgBuf; > >>>>>> > >>>>>> bluefish has: > >>>>>> > >>>>>> struct small_msgbuf { > >>>>>> long mtype; > >>>>>> char mtext[MSQ_QUEUE_SMALL_SIZE]; > >>>>>> } small_msgp; > >>>>>> > >>>>>> > >>>>>> My laptop has nothing at all in /dev/mqueue. > >>>>>> > >>>>>> So I don't really know what the right thing to do is. Certainly if > >>>>>> we're going to apply this patch, we should also fix the header. I > >>>>>> almost think we should *delete* struct msgbuf from the headers, since > >>>>>> it's all kinds of busted, but that will break the NTP build. Ideally > >>>>>> we would go back in time and remove it from the headers. > >>>>>> > >>>>>> Libc people, any insight? We can probably fix the bug without > >>>>>> annoying anyone given how lightly x32 is used and how lightly POSIX > >>>>>> message queues are used. > >>>>> > >>>>> If it's that outright wrong and always has been, I feel like the old > >>>>> syscall numbers should just be deprecated and new ones assigned. > >>>>> Otherwise, there's no way for userspace to be safe against data > >>>>> corruption when run on older kernels. If there's a new syscall number, > >>>>> libc can just use the new one unconditionally (giving ENOSYS on > >>>>> kernels where it would be broken) or have a x32-specific > >>>>> implementation that makes the old syscall and performs translation if > >>>>> the new one fails with ENOSYS. > >>>> > >>>> That doesn't really help broken code continue to work reliably, as > >>>> upgrading libc will just pull in the new syscall for a binary that's > >>>> expecting the broken behaviour, unless you do symbol versioning, but > >>>> then it'll just break when you next recompile the code, and there's no > >>>> way for that to be diagnosed given the *application* has to define the > >>>> type. But given it's application-defined I really struggle to see how > >>>> any code out there is actually expecting the current x32 behaviour as > >>>> you'd have to go really out of your way to find out that x32 is broken > >>>> and needs __kernel_long_t. I don't think there's any way around just > >>>> technically breaking ABI whilst likely really fixing ABI in 99.999% of > >>>> cases (maybe 100%). > >>> > >>> I'm not opposed to "breaking ABI" here because the current syscall > >>> doesn't work unless someone wrote bogus x32-specific code to work > >>> around it being wrong. I don't particularly want to preserve any of > >>> the current behavior. > >>> > >>> What I am somewhat opposed to is making a situation where an updated > >>> libc can't be safe against getting run on a kernel with a broken > >>> version of the syscall and silently corrupting data. I'm flexible > >>> about how avoiding tha tis achieved. > >> > >> If we're sufficiently confident that we won't regress anything by > >> fixing the bug, I propose we do the following. First, we commit a fix > >> that's Jessica's patch plus a fix to struct msghdr, and we mark that > >> for -stable. Then we commit another patch that removes 'struct > >> msghdr' from uapi entirely, but we don't mark that for -stable. If > >> people complain about the latter, we revert it. > > > > Thinking about this more, MIPS n32 is also affected by that header. In > > fact the n32 syscalls currently do the right thing and use the compat > > implementations, so the header is currently out-of-sync with the kernel > > there*. This should be noted when committing the change to msg.h. > > Never mind, it seems MIPS n32 is weird and leaves __kernel_long_t as a > normal long despite being an ILP32-on-64-bit ABI, I guess because it's > inherited from IRIX rather than being invented by the GNU world. Yes, the whole __kernel_long_t invention is largely x32-only (maybe theoretically on aarch64-ilp32 too? if that even really exists?) and is pretty much entirely a mistake from lacking the proper infrastructure to do time64 when x32 was introduced (note that n32 has 32-bit old-time_t). I hope effort will be made to keep the same mistake from creeping into future ilp32-on-64 ABIs if there are any. Rich