Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp862613lqo; Wed, 8 May 2024 18:43:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXRhgL0HAf+Xb7IWFAXMeGmU0Y8Pwg1Aom9CHqaSebgXxQ0vxJ+flizf05T8RZ3eGTfQ0WNGyXdgu9Tzb1HujxJfeVWwRhbA9BYQiu15A== X-Google-Smtp-Source: AGHT+IHawg9doJLsNnSx0vJWelsULlSyrWKDM5mVidMLHC/UmSbgE+Xr9TVzRF7eRqkE/2cZh6pY X-Received: by 2002:a05:6214:20c9:b0:69f:a5a5:4eeb with SMTP id 6a1803df08f44-6a15147cb20mr48114146d6.38.1715218999164; Wed, 08 May 2024 18:43:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715218999; cv=pass; d=google.com; s=arc-20160816; b=oHnGi5yd0PrYpVOcRk6R5OYeb2qAClOh4cPOa1XKwi5NsNRMQ8AV2cWcPnYY30musc KHCIiMfLO2UWVQS8MHXib/PKoF4wRf5KsyD24RE47B9hatszQ/pSRtq3rf7bAQcDbNB1 CerwVxvS5ANjXR0e1bBKvhcMKtLrofGI7hs3iSAYpK9RF30e4oylQ2Ik7Y1Qtjq2eO9t BgR8NrvCOP5ZR9DTwq0w/jX3Jx6zZZOyhcmCrh/Yh4tSMunjMnu9SM0/49eWhf/ELqGu CijBvBfruWCXJa0hh5s3UFFp/jVAXig/6eM0a15QP6M1O+pqQ2I2EkdOUq4jErejeUcn PsrQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=jjW7YSKOnyy4HDgHwFaP+V9/m7qnEx/jJqBTszrsdI4=; fh=+gZcglhBgY+nN4LMTbqC3OmlBiE4xsic29dwxq9fOXU=; b=VmjYNuJqRPQL7zPTQIFRKD7tlNVcLXgRGad6D9woPed9cBfBlyzk1DfL1Uo8SJNnqN +zccGTIFUapenyWhLXnb2U6lihd5l/dQZ5aYgAB7dJIa1taCf0K8FXCGocHvIKya9LTJ 9AUgDjWrd0dJ4t5n4ZGH9NxZxIGCZr/IvZ/mixGL8O6kxKd763XkKtrigZ4WgAD6l/7L t6SSrYA9A7QtCkyb9HV4e92365Muyz9AXGo6hYdQyXTy5+DOUw3M0ALmOUDwH3sgwzPc 873fKmUXqNJxML11happJ4eyOWB+CvpNOECvZaEeMOP8z7OGbmIKrUiFhAWKNjmTIw/y VU6A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="jm/3O7F3"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-174023-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174023-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6a15f305030si4045246d6.557.2024.05.08.18.43.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 18:43:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174023-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="jm/3O7F3"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-174023-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174023-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DDB111C2112B for ; Thu, 9 May 2024 01:43:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 800DC13C3ED; Thu, 9 May 2024 01:43:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jm/3O7F3" Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 4FBFB2C184; Thu, 9 May 2024 01:43:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715218992; cv=none; b=AQSmbDXwCSX9cPaGLkXTTUqXhhEdMgRyem6aYxXTiymqRdt0AdNxQkYTbADVAFuthTRzPQIGI8/fT9cVwraHA9ucddsKKG5zIKeQHr06ke/BzBdywFPY7j07nLpBHEsQqIbdnKuX3YOaFsZJ5yU2HzhNQg54+zhAovqqr/Xnn+M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715218992; c=relaxed/simple; bh=h4j9talqhyuZO0E63OwVJsILnahc4H2nREvymN3UlsM=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=ZQ2/vlqsbeV2TGgHFUuKQNSrsFzSw5jmAkiu3X+H49IfhTQNk6+PpI/iJ0GTd5ZWoenDaEhYKRUNEBmdIy07jq1VfT/EPRQEBJFV0jf/LLdbHkTTbBtMUJ4j1bF8tAMxSB4tgiccvmhiMIuFQ+x9qYmw/O35siCo4xFUBqlk2xs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jm/3O7F3; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6f4521ad6c0so376134b3a.0; Wed, 08 May 2024 18:43:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715218990; x=1715823790; 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=jjW7YSKOnyy4HDgHwFaP+V9/m7qnEx/jJqBTszrsdI4=; b=jm/3O7F3zAAiZopNEw7ksc2nKKPFDRFQj3QIccLHVwARNh0+4DEczQgPpYk5gtgnPK I9oZvFDXWXRKEWOCv1adxXYmSPdzOv+wSar/UeAO6B6TB2eHF+rKOPNoAdkbqDTo7Ebm TqlibBPk8b8cURsO1EjF7lJ8qNQGy9JPXTuBpYB/pi6mX6wrR7TG/0hQH9XSZqR7Fh72 2zwvwXA6QjtAw6vA1fAH2aF12oz2+zQW2Ik05A0NQN3D/i4vmu+dc1e1YJLe20sYPMz0 +66WXLuD8CJUnOGcC6Os4v/Mifuf2mu+k+qNFEPo+1uAKKw/CrNww2hfgq55j1NCA61R fI4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715218990; x=1715823790; 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=jjW7YSKOnyy4HDgHwFaP+V9/m7qnEx/jJqBTszrsdI4=; b=JmEo3DMQl6Xp4iO5BWeYCPgXr0xE0Ca30CU00L+uignb31/36S342+9/28/dobS6Hg xEtPNztOugbOEZ8chbE4wiAWVisfMn6IciWBGiF8m3BcRMWPANyC95g8pFTIgvxtGU0G pXE04zur7Zq+D6EJ8MDEklH+e/yVKpQFKojEj5DGl+UCqK3fd5SiVGZtk4Z3InKwMfWL Fdj8qkqhdIW3Coc6PWogyilgvjDI4kQzA8uTgrWtOk/ZurUUDrfz4qjrysVuO9H4ufgZ orHecx3mq7OkymfXGg9mjJMbeYR2yv8c7WgVg8wX+c7KMmEFiPvVKD0cbVseE76hq2DX RHVA== X-Forwarded-Encrypted: i=1; AJvYcCVQZHQSxfIXu/BmzJiTmqroDivP2M8TawpsLzcSEQbn/pUYLB5TU2ElKxWmP5k8HFckhM07bbCjExUy8KtKvlchPSZQc83MxNNiTr3/Q5QnOADaGcH8FnnmGKxIAXAHnbpHc7Gfg2wpwSrtFbk= X-Gm-Message-State: AOJu0YxJbDd9z0n7aYYu611pkEiBFlurwweKnL7dAxHFLb3aWkh8BKk1 T/qj+YS7Koe4QR02bBAqj531VUDPVTt/N9Om9fd1IU+jKOREkSgG X-Received: by 2002:a05:6a20:3257:b0:1af:6a37:3b8a with SMTP id adf61e73a8af0-1afc8d26c83mr4209898637.16.1715218990533; Wed, 08 May 2024 18:43:10 -0700 (PDT) Received: from localhost.localdomain ([190.196.103.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2b2d995sm204497b3a.192.2024.05.08.18.43.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 18:43:09 -0700 (PDT) From: Camila Alvarez To: Kent Overstreet Cc: Brian Foster , linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org, Camila Alvarez , syzbot+9833a1d29d4a44361e2c@syzkaller.appspotmail.com Subject: [PATCH] bcachefs: guard against invalid bits_per_field in bch2_bkey_format_invalid Date: Wed, 8 May 2024 21:40:32 -0400 Message-Id: <20240509014030.671222-1-cam.alvarez.i@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A check for a valid value for bits_per_field is performed for each field before computing packed_max. If bits_per_field is invalid for any field the whole format is deemed invalid. Reported-by: syzbot+9833a1d29d4a44361e2c@syzkaller.appspotmail.com Signed-off-by: Camila Alvarez --- fs/bcachefs/bcachefs_format.h | 8 ++++++++ fs/bcachefs/bkey.c | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/fs/bcachefs/bcachefs_format.h b/fs/bcachefs/bcachefs_format.h index d5b90439e581..9e688a8d780f 100644 --- a/fs/bcachefs/bcachefs_format.h +++ b/fs/bcachefs/bcachefs_format.h @@ -318,6 +318,14 @@ enum bch_bkey_fields { #define bkey_format_field(name, field) \ [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8) +#define BCH_BKEY_FIELDS() \ + x(INODE, p.inode) \ + x(OFFSET, p.offset) \ + x(SNAPSHOT, p.snapshot) \ + x(SIZE, size) \ + x(VERSION_HI, version.hi) \ + x(VERSION_LO, version.lo) + #define BKEY_FORMAT_CURRENT \ ((struct bkey_format) { \ .key_u64s = BKEY_U64s, \ diff --git a/fs/bcachefs/bkey.c b/fs/bcachefs/bkey.c index 76e79a15ba08..f7847c96a105 100644 --- a/fs/bcachefs/bkey.c +++ b/fs/bcachefs/bkey.c @@ -638,6 +638,13 @@ struct bkey_format bch2_bkey_format_done(struct bkey_format_state *s) return ret; } +static unsigned bch2_max_bits_per_field[] = { +#define x(name, field) \ + bkey_format_field(name, field), + BCH_BKEY_FIELDS() +#undef x +}; + int bch2_bkey_format_invalid(struct bch_fs *c, struct bkey_format *f, enum bkey_invalid_flags flags, @@ -659,8 +666,15 @@ int bch2_bkey_format_invalid(struct bch_fs *c, if (!c || c->sb.version_min >= bcachefs_metadata_version_snapshot) { unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i]; u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1)); - u64 packed_max = f->bits_per_field[i] - ? ~((~0ULL << 1) << (f->bits_per_field[i] - 1)) + unsigned bits_per_field = f->bits_per_field[i]; + + if (bits_per_field > bch2_max_bits_per_field[i]) { + prt_printf(err, "field %u uses more bits than allowed: %u > %u", + i, bits_per_field, bch2_max_bits_per_field[i]); + return -BCH_ERR_invalid; + } + u64 packed_max = bits_per_field + ? ~((~0ULL << 1) << (bits_per_field - 1)) : 0; u64 field_offset = le64_to_cpu(f->field_offset[i]); -- 2.34.1