From patchwork Tue Jul 30 20:55:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thorsten Blum X-Patchwork-Id: 13747888 Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD56E1AA3C3 for ; Tue, 30 Jul 2024 20:57:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722373072; cv=none; b=uabxDuDhnmfPr+h30BUdTBwffbK9ET1PbUn6RCcQQCfLe4IQYJYlttrlVJc18V5cXbqjFNKB4NS6Sk9GMhnncOdtVn84/Fd3CvhadX5Bb+4qMmc5JZ+2r6Y2GWYWyRF2cd/ugjEtU8YM7OjbwjZHCKqGkRGBUqQwIGU0KrFDDT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722373072; c=relaxed/simple; bh=UZFioN48C+LMU6bnsXcw2rpATfhSP3Md7LINlbLSdyQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=bMqtSVbq5errRCzoQAOvtbJzuRCatz7k8TDaMhPf6Y+mEgbBthM9ASS9gtKdCqzPJGbwNWz1VTFc+U2ap+Rqt1QRLVbcp46Q4kthwAq4bTT7Zw5Mf7OsFM3K0CVJpMyw8I8tkp7qXnzQp5D1gMdxXXuIMl9STmISbrRX3004TC8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toblux.com; spf=none smtp.mailfrom=toblux.com; dkim=pass (2048-bit key) header.d=toblux-com.20230601.gappssmtp.com header.i=@toblux-com.20230601.gappssmtp.com header.b=Gr8wkIca; arc=none smtp.client-ip=209.85.208.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toblux.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toblux.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toblux-com.20230601.gappssmtp.com header.i=@toblux-com.20230601.gappssmtp.com header.b="Gr8wkIca" Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2f025b94e07so66509731fa.0 for ; Tue, 30 Jul 2024 13:57:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toblux-com.20230601.gappssmtp.com; s=20230601; t=1722373069; x=1722977869; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qPxJ/YjE+pYwHZd7W0h0eHbps73SI/HtbDFZ2gy9/OI=; b=Gr8wkIcaLpeJqscLsXsqh3LH3h8gfKnos6InjG9Lo2WXps2bOLRiAANZqmqoxGmd4l l6uIPWj5NkrpePQkk3bQQQGjWDS0yFEcFfbwQZy5vCaxt897Nc8b8u9M7ZQubHVpvz8s jwaF11Oa0NlproLgMcjaqk94XQDMpBt98K9P9Y+R2jEvxqogOEW5JrLeBkjf1kndYDA/ h6WfhxLktE2m0mo7YqAFRc+2hyKpv0FhA72QwBC9/360eHckXFWg25Ntdwf45nCm6806 w+oc7fc30FGwxIEv1YlsmHl1OhgUy5NMU0PwHEZOL0sYZ2u8EZz1GVdgQnLmLQAgV7EM +cjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722373069; x=1722977869; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qPxJ/YjE+pYwHZd7W0h0eHbps73SI/HtbDFZ2gy9/OI=; b=bpUIQL0jQZMidcZq9BzoO9BhpqyjRk82/jLw5qIi1T9Uy31XOo+Y2WhX2jYttJZAK0 6qRQ0oyhbDlfuKIjwArfbsTjkVdfA4XcPGIbDY1D3eVpplgDC9Jci+osYhvq/RNiRBjI 0PTnfgQAiHdtxphL3UQkDPSSLWubyC8ENioZ4M5xoh4LTB1PbL9cPUik3MpNFH+9ldKv Wmz2qReUIWEu6m7s8yIfL/u1J03QTCylwNRDWhHabJ+N/XnZWcT9t4PU2iM9lEYYRZ/8 Gp5IdDkQWBz4GO7oFmOHgyHfgEx9YIZNMF2es9eHTKfkdVN/5UXvenZVuBpGWtBvsCga a0IQ== X-Forwarded-Encrypted: i=1; AJvYcCU3HUBCe/WsIe3sF/2iVbSmo/EiivpZVSsmKdVfKxbyaEKgL1G9xHFWrwkLwGANmk7MBp8TDLdCVInbuPJfXIQB/Qra8SEmFfUPie/W/BF9 X-Gm-Message-State: AOJu0Yye/XW3lLGydP2w5A0gUdRbx9L97LMwJd9E+Bu4A9lJwQZwP7U6 Optam4u8fSGSZ146a2rRMZf7YFb/HgRIZ+W26WJSawByLBD05KmBuNjmduBVuz8= X-Google-Smtp-Source: AGHT+IHzJtr5q5xSTOQZVFeHBzvGrYkdr2BxlIJK77NCkkzfZzn1C7QUQedTZgZbk/bUkOQzMVxrkA== X-Received: by 2002:a2e:8007:0:b0:2ef:1ecc:cf5a with SMTP id 38308e7fff4ca-2f12edd6ccamr86539441fa.26.1722373068570; Tue, 30 Jul 2024 13:57:48 -0700 (PDT) Received: from fedora.fritz.box (aftr-82-135-80-26.dynamic.mnet-online.de. [82.135.80.26]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac6377e06fsm7801393a12.28.2024.07.30.13.57.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 13:57:48 -0700 (PDT) From: Thorsten Blum To: tytso@mit.edu, adilger.kernel@dilger.ca, kees@kernel.org, gustavoars@kernel.org Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Thorsten Blum Subject: [PATCH v3] ext4: Annotate struct ext4_xattr_inode_array with __counted_by() Date: Tue, 30 Jul 2024 22:55:11 +0200 Message-ID: <20240730205509.323320-3-thorsten.blum@toblux.com> X-Mailer: git-send-email 2.45.2 Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add the __counted_by compiler attribute to the flexible array member inodes to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE. Remove the now obsolete comment on the count field. Refactor ext4_expand_inode_array() by assigning count before copying any data using memcpy(). Use struct_size() instead of offsetof() and remove the local variable count. Increment count before adding a new inode to the inodes array. Compile-tested only. Signed-off-by: Thorsten Blum --- Changes in v2: - Adjust ext4_expand_inode_array() as suggested by Gustavo A. R. Silva - Use struct_size() and struct_size_t() instead of offsetof() - Link to v1: https://lore.kernel.org/linux-kernel/20240729110454.346918-3-thorsten.blum@toblux.com/ Changes in v3: - Use struct_size() instead of struct_size_t() as suggested by Kees Cook - Remove the local variable count as suggested by Gustavo A. R. Silva - Increment count before adding a new inode as suggested by Gustavo - Link to v2: https://lore.kernel.org/linux-kernel/20240730172301.231867-4-thorsten.blum@toblux.com/ --- fs/ext4/xattr.c | 24 +++++++++++------------- fs/ext4/xattr.h | 4 ++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c index 46ce2f21fef9..4e20ef7cc502 100644 --- a/fs/ext4/xattr.c +++ b/fs/ext4/xattr.c @@ -2879,33 +2879,31 @@ ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, if (*ea_inode_array == NULL) { /* * Start with 15 inodes, so it fits into a power-of-two size. - * If *ea_inode_array is NULL, this is essentially offsetof() */ - (*ea_inode_array) = - kmalloc(offsetof(struct ext4_xattr_inode_array, - inodes[EIA_MASK]), - GFP_NOFS); + (*ea_inode_array) = kmalloc( + struct_size(*ea_inode_array, inodes, EIA_MASK), + GFP_NOFS); if (*ea_inode_array == NULL) return -ENOMEM; (*ea_inode_array)->count = 0; } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { /* expand the array once all 15 + n * 16 slots are full */ struct ext4_xattr_inode_array *new_array = NULL; - int count = (*ea_inode_array)->count; - /* if new_array is NULL, this is essentially offsetof() */ new_array = kmalloc( - offsetof(struct ext4_xattr_inode_array, - inodes[count + EIA_INCR]), - GFP_NOFS); + struct_size(*ea_inode_array, inodes, + (*ea_inode_array)->count + EIA_INCR), + GFP_NOFS); if (new_array == NULL) return -ENOMEM; - memcpy(new_array, *ea_inode_array, - offsetof(struct ext4_xattr_inode_array, inodes[count])); + new_array->count = (*ea_inode_array)->count; + memcpy(new_array, *ea_inode_array, + struct_size(new_array, inodes, new_array->count)); kfree(*ea_inode_array); *ea_inode_array = new_array; } - (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode; + (*ea_inode_array)->count++; + (*ea_inode_array)->inodes[(*ea_inode_array)->count - 1] = inode; return 0; } diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h index bd97c4aa8177..e14fb19dc912 100644 --- a/fs/ext4/xattr.h +++ b/fs/ext4/xattr.h @@ -130,8 +130,8 @@ struct ext4_xattr_ibody_find { }; struct ext4_xattr_inode_array { - unsigned int count; /* # of used items in the array */ - struct inode *inodes[]; + unsigned int count; + struct inode *inodes[] __counted_by(count); }; extern const struct xattr_handler ext4_xattr_user_handler;