Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp11462lfv; Tue, 12 Apr 2022 15:12:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxkZHoTS4/+vPpX86uWNdwRe0YGSYpQX+p2dTHfXcYL4wXZ8YWMQcrsv/bVWaBJbHfYmSmr X-Received: by 2002:a17:902:ce02:b0:151:a86d:dc2b with SMTP id k2-20020a170902ce0200b00151a86ddc2bmr40333228plg.57.1649801554963; Tue, 12 Apr 2022 15:12:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649801554; cv=none; d=google.com; s=arc-20160816; b=j3mAsQastOY+Xc+vC2qRWb/ySV7QYv5HnEo/aaAPFQ9MDYrUbDmhxAbwyspZFJA5CJ uF681nvZaEBoodL7kH8bw4tpw+Kp4QuJSYx21dSNKxyiX+Yx+c0CRAj0sKh81kTEFQRL JjjabVX/Qv3s9t3Ru78RsimqpB1Mkio2ZGMoXeEV24Qej/jepoA6sgahJOSgwdUppS4a J5k3NmCXXY5ZNpG7mrSaGvtG7rdIaBPT4ngEv77XZ6hLnH93kJ0IkBtMbtgW/C+9EP9W czmL8x6o+Kc3eLjAMNXAzRGxtK3fOSlPwLJn43d5ug0ak0JjFzZCc7xO7TYRsUi8G6oV dJ1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=qo34ra1Ctd2P2g3kRmkGRG2uf3Jj3c0FkXva368F/aw=; b=XvNLr3WOV0u39FKJUV+dgEP0jSUWaORWM51z115qZhrsUfwBHLHOor1/YbljpkOsrA 2tDumU0KwWz1XhGiW3NU3y0sKjcp35m4ntYE81Rbe+dEaV+0hSv2byKHBEYSoIBk3hHZ 9K+9+zSnjl5/TKM3KWdON5+PrTC1yHuAdpMx9XT4EY/oUx4rAst4RBZWvjxcx48cSKna fzTzwHZTujOQiwAvW2C8I3btTQKwLQyJjskcqFMK6VhF8loBkfq1E0U52zN24AvP0Bpw jcDvKtpjUnMAMiCwMGsAhHwK2TXo7u/v0DxzqB3AQlzqNwhyHIMSSMIO85zT/qJCiHfn PQ8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EH2p0voQ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e16-20020a17090301d000b00153b2d16582si14307683plh.394.2022.04.12.15.12.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 15:12:34 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EH2p0voQ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B37C71777F7; Tue, 12 Apr 2022 14:02:59 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245428AbiDLKr3 (ORCPT + 99 others); Tue, 12 Apr 2022 06:47:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356456AbiDLKpY (ORCPT ); Tue, 12 Apr 2022 06:45:24 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7A766692BE for ; Tue, 12 Apr 2022 02:41:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649756465; 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: in-reply-to:in-reply-to:references:references; bh=qo34ra1Ctd2P2g3kRmkGRG2uf3Jj3c0FkXva368F/aw=; b=EH2p0voQRmm8b/S5Q0q5H1vROVZxVPjsuUw0/75xnhl9dAWWESnQ+nCjgpedAR1NPmJ1AK 4fAtfPzDtDgGwsMYps4nIxofcWARQUockyyMEF0yF5hXElVkiknpq744GROwlyF0ZAR0Nn k38Z4mirCRVevqLszXsstmOGJ314+kA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-461-bjX97eMDMRmKjhEClZCRng-1; Tue, 12 Apr 2022 05:41:02 -0400 X-MC-Unique: bjX97eMDMRmKjhEClZCRng-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 830DD1C1C521; Tue, 12 Apr 2022 09:41:01 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 66FC540CFD1D; Tue, 12 Apr 2022 09:41:01 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 23C9f12Y021797; Tue, 12 Apr 2022 05:41:01 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 23C9f0pU021793; Tue, 12 Apr 2022 05:41:00 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Tue, 12 Apr 2022 05:41:00 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Linus Torvalds cc: Alexander Viro , linux-fsdevel , Linux Kernel Mailing List , Matthew Wilcox Subject: [PATCH] stat: fix inconsistency between struct stat and struct compat_stat In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Mon, 11 Apr 2022, Linus Torvalds wrote: > On Mon, Apr 11, 2022 at 7:13 AM Mikulas Patocka wrote: > > > > Should we perhaps hash the number, take 16 bits of the hash and hope > > than the collision won't happen? > > That would "work", but I think it would be incredibly annoying to > users with basically random results. > > I think the solution is to just put the bits in the high bits. Yes, > they might be masked off if people use 'MAJOR()' to pick them out, but > the common "compare st_dev and st_ino" model at least works. That's > the one that wants unique numbers. > > > For me, the failure happens in cp_compat_stat (I have a 64-bit kernel). In > > struct compat_stat in arch/x86/include/asm/compat.h, st_dev and st_rdev > > are compat_dev_t which is 16-bit. But they are followed by 16-bit > > paddings, so they could be extended. > > Ok, that actually looks like a bug. > > The compat structure should match the native structure. Those "u16 > __padX" fields seem to be just a symptom of the bug. > > The only user of that compat_stat structure is the kernel, so that > should just be fixed. > > Of course, who knows what the libraries have done, so user space could > still have screwed up. Here I'm sending a patch that makes struct compat_stat match struct stat. stat: fix inconsistency between struct stat and struct compat_stat struct stat (defined in arch/x86/include/uapi/asm/stat.h) has 32-bit st_dev and st_rdev; struct compat_stat (defined in arch/x86/include/asm/compat.h) has 16-bit st_dev and st_rdev followed by a 16-bit padding. This patch fixes struct compat_stat to match struct stat. Note that we can't change compat_dev_t because it is used by compat_loop_info. Also, if the st_dev and st_rdev values are 32-bit, we don't have to use old_valid_dev to test if the value fits into them. This fixes -EOVERFLOW on filesystems that are on NVMe because NVMe uses the major number 259. Signed-off-by: Mikulas Patocka --- arch/x86/include/asm/compat.h | 6 ++---- fs/stat.c | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) Index: linux-5.17.2/arch/x86/include/asm/compat.h =================================================================== --- linux-5.17.2.orig/arch/x86/include/asm/compat.h 2022-01-21 10:29:12.000000000 +0100 +++ linux-5.17.2/arch/x86/include/asm/compat.h 2022-04-12 11:27:14.000000000 +0200 @@ -28,15 +28,13 @@ typedef u16 compat_ipc_pid_t; typedef __kernel_fsid_t compat_fsid_t; struct compat_stat { - compat_dev_t st_dev; - u16 __pad1; + u32 st_dev; compat_ino_t st_ino; compat_mode_t st_mode; compat_nlink_t st_nlink; __compat_uid_t st_uid; __compat_gid_t st_gid; - compat_dev_t st_rdev; - u16 __pad2; + u32 st_rdev; u32 st_size; u32 st_blksize; u32 st_blocks; Index: linux-5.17.2/fs/stat.c =================================================================== --- linux-5.17.2.orig/fs/stat.c 2022-04-12 10:39:46.000000000 +0200 +++ linux-5.17.2/fs/stat.c 2022-04-12 10:58:28.000000000 +0200 @@ -334,9 +334,6 @@ SYSCALL_DEFINE2(fstat, unsigned int, fd, # define choose_32_64(a,b) b #endif -#define valid_dev(x) choose_32_64(old_valid_dev(x),true) -#define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x) - #ifndef INIT_STRUCT_STAT_PADDING # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st)) #endif @@ -345,7 +342,9 @@ static int cp_new_stat(struct kstat *sta { struct stat tmp; - if (!valid_dev(stat->dev) || !valid_dev(stat->rdev)) + if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev)) + return -EOVERFLOW; + if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev)) return -EOVERFLOW; #if BITS_PER_LONG == 32 if (stat->size > MAX_NON_LFS) @@ -353,7 +352,7 @@ static int cp_new_stat(struct kstat *sta #endif INIT_STRUCT_STAT_PADDING(tmp); - tmp.st_dev = encode_dev(stat->dev); + tmp.st_dev = new_encode_dev(stat->dev); tmp.st_ino = stat->ino; if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) return -EOVERFLOW; @@ -363,7 +362,7 @@ static int cp_new_stat(struct kstat *sta return -EOVERFLOW; SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); - tmp.st_rdev = encode_dev(stat->rdev); + tmp.st_rdev = new_encode_dev(stat->rdev); tmp.st_size = stat->size; tmp.st_atime = stat->atime.tv_sec; tmp.st_mtime = stat->mtime.tv_sec; @@ -644,11 +643,13 @@ static int cp_compat_stat(struct kstat * { struct compat_stat tmp; - if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev)) + if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev)) + return -EOVERFLOW; + if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev)) return -EOVERFLOW; memset(&tmp, 0, sizeof(tmp)); - tmp.st_dev = old_encode_dev(stat->dev); + tmp.st_dev = new_encode_dev(stat->dev); tmp.st_ino = stat->ino; if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) return -EOVERFLOW; @@ -658,7 +659,7 @@ static int cp_compat_stat(struct kstat * return -EOVERFLOW; SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); - tmp.st_rdev = old_encode_dev(stat->rdev); + tmp.st_rdev = new_encode_dev(stat->rdev); if ((u64) stat->size > MAX_NON_LFS) return -EOVERFLOW; tmp.st_size = stat->size;