Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1365324rwd; Thu, 1 Jun 2023 14:37:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ62J1Ba5l3HodayfI2WDIulvx4PAyB8v8Vl/TD7zXKQFc3Zy1VrPbM6RutQHKTofKZGtEJ6 X-Received: by 2002:a05:6a00:1399:b0:63b:8afe:a4dc with SMTP id t25-20020a056a00139900b0063b8afea4dcmr9725730pfg.30.1685655447846; Thu, 01 Jun 2023 14:37:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685655447; cv=none; d=google.com; s=arc-20160816; b=c4EZaiv/FoaUhN07vRECcAOjerCTNNe8GW/zoA5RulPf7HBzBfTBqo0Yg0C+qckxGt xITlYUO4mXmPdMiMyjIoNqJlllV09I7oFZOaL661TDOtqrMVZ5TsMIvzU5txBPJge7MN olRlXT+lrAoLLAOSzn+RO+7Lv2AXVcLR9eS9/MJ7uJ28FxTFtNwyUNWDcnydhme5ac/R 2nAcfSO78X3iopi2aRTDE1UP7v0zzc5mFFsihOJTHtPcg9KPgEXx44myj9zTCOux4GUz 40EL4296rLUiROpPVV12Rt0VFQmipJ/jklI5pa+4CvZieoAGd4romI9k6LbSE7kt1DrC YkgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=YZHd7YYjlDK6sv+mkKSJgYpLNyemRttpNw5QnchM4bE=; b=bRjjrWQspVrYvyoEJ8XI+NU/Syve88UPHxWJy2HHo5ARGlDEG9yNEDrxjYo4c12EsF IZ6RavFnO0yrshvQgs9jGbbii9OmlWAKlMAMLeaqsOEMu2tai6QTwN3QixFuDPQpbVs7 jW7XxFvQAHX6CQz6xtxZI0+RgwPill16CIgWrlZJpFZbD2heyNSpTTQtVsvj/5LYlW7/ lcngFS1d7PlU4U0XfI2Y76pyanUJDiHSHhjtQ1dDQjr5WBY6unKpuyJjxnYOqU7nc3// H0ufEHiRl6OpRkbWyHLPkpJK0uoP06hZS1DWp2ZwLbvRxKeDcK8rKpRygNtDN21L7O+w QHaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@invisiblethingslab.com header.s=fm1 header.b=kS5wGTsh; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=BjqUds5O; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f14-20020aa7968e000000b0063f032d78efsi6076159pfk.269.2023.06.01.14.37.11; Thu, 01 Jun 2023 14:37:27 -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=@invisiblethingslab.com header.s=fm1 header.b=kS5wGTsh; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=BjqUds5O; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231790AbjFAVZ1 (ORCPT + 99 others); Thu, 1 Jun 2023 17:25:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231499AbjFAVZU (ORCPT ); Thu, 1 Jun 2023 17:25:20 -0400 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B7B6196; Thu, 1 Jun 2023 14:25:19 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id ED3F95C01D0; Thu, 1 Jun 2023 17:25:18 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 01 Jun 2023 17:25:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= invisiblethingslab.com; h=cc:cc:content-transfer-encoding :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm1; t=1685654718; x=1685741118; bh=YZHd7YYjlD K6sv+mkKSJgYpLNyemRttpNw5QnchM4bE=; b=kS5wGTshap2i++C5h1+TcSncc2 FyNT5Ls4NghWOFzLklCX4jfQm1SrfiXo66GC2isWcdxhTthn0WyMAHJaWEIeAz+d BeX9M9RDPL9Y32OquGNX3RVXOvnLAhrGYDV+GrhQoe7w6g3Bo4YXRwDHWmbrjnY0 WNNLkrPKPXI5Q74v546DBQ2iNrddhKnRHV9xPTCveDoRefQPpBU4KmQqdG48oJ/6 G/W62z6gUVGPcamLkxaQUnvUXev+4NevkSbPwuH+6MD7sjFaBPbCTNg13yhHJ2EZ 7tdxmXFToLqpFMwqVK6xCQjEK7EwvWniq9DdGBOQLTmBrufHLPxrPaSi6FAQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1685654718; x= 1685741118; bh=YZHd7YYjlDK6sv+mkKSJgYpLNyemRttpNw5QnchM4bE=; b=B jqUds5OJLEiOrCvY8DDYia28d8+AGuiTY0nV6LPPKaTx+2zjEBJoQsS49+hlmZjI CCTkhS56YM2FuYbq7EQm58qze9jCa9ZEs/C13ZOvxaTI9Tl8pbvJ/knc4WZznSHQ MtZATudiq2KU6e7ye3tVGSloyyUNvbAW4ELQtPc0M/qLLs3q2CaW1WRBau8g0Zvz wwGCKiBv9yz6zE7qcgA3i62dbnnrgrNXScb/hXZjeWqxGtz9+SC7jIZY8C9L9uu1 Ydnv/N9NaPZM3jBZJ3mozb75d8Nt6Lv4EhXSHAlAXkNYFIosfaC3dGicb470Oi98 r+T0c9ScG7TQmBXyjXHMQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeeluddgudeiudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffvghm ihcuofgrrhhivgcuqfgsvghnohhurhcuoeguvghmihesihhnvhhishhisghlvghthhhinh hgshhlrggsrdgtohhmqeenucggtffrrghtthgvrhhnpeejffejgffgueegudevvdejkefg hefghffhffejteekleeufeffteffhfdtudehteenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpeguvghmihesihhnvhhishhisghlvghthhhinhhg shhlrggsrdgtohhm X-ME-Proxy: Feedback-ID: iac594737:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 1 Jun 2023 17:25:18 -0400 (EDT) From: Demi Marie Obenour To: Alasdair Kergon , Mike Snitzer , dm-devel@redhat.com Cc: Demi Marie Obenour , linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH 3/6] device-mapper: structs and parameter strings must not overlap Date: Thu, 1 Jun 2023 17:24:52 -0400 Message-Id: <20230601212456.1533-4-demi@invisiblethingslab.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230601212456.1533-1-demi@invisiblethingslab.com> References: <20230601212456.1533-1-demi@invisiblethingslab.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE 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 The NUL terminator for each target parameter string must precede the following 'struct dm_target_spec'. Otherwise, dm_split_args() might corrupt this struct. Furthermore, the first 'struct dm_target_spec' must come after the 'struct dm_ioctl', as if it overlaps too much dm_split_args() could corrupt the 'struct dm_ioctl'. Signed-off-by: Demi Marie Obenour Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org --- drivers/md/dm-ioctl.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c index 64e8f16d344c47057de5e2d29e3d63202197dca0..da6ca26b51d0953df380582bb3a51c2ec22c27cb 100644 --- a/drivers/md/dm-ioctl.c +++ b/drivers/md/dm-ioctl.c @@ -1391,7 +1391,7 @@ static inline fmode_t get_mode(struct dm_ioctl *param) return mode; } -static int next_target(struct dm_target_spec *last, uint32_t next, void *end, +static int next_target(struct dm_target_spec *last, uint32_t next, const char *end, struct dm_target_spec **spec, char **target_params) { static_assert(_Alignof(struct dm_target_spec) <= 8, @@ -1404,7 +1404,7 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, * sizeof(struct dm_target_spec) or more, as otherwise *last was * out of bounds already. */ - size_t remaining = (char *)end - (char *)last; + size_t remaining = end - (char *)last; /* * There must be room for both the next target spec and the @@ -1423,10 +1423,7 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec + 1); - if (*spec < (last + 1)) - return -EINVAL; - - return invalid_str(*target_params, end); + return 0; } static int populate_table(struct dm_table *table, @@ -1436,8 +1433,9 @@ static int populate_table(struct dm_table *table, unsigned int i = 0; struct dm_target_spec *spec = (struct dm_target_spec *) param; uint32_t next = param->data_start; - void *end = (void *) param + param_size; + const char *const end = (const char *) param + param_size; char *target_params; + size_t min_size = sizeof(struct dm_ioctl); if (!param->target_count) { DMERR("%s: no targets specified", __func__); @@ -1445,6 +1443,13 @@ static int populate_table(struct dm_table *table, } for (i = 0; i < param->target_count; i++) { + const char *nul_terminator; + + if (next < min_size) { + DMERR("%s: next target spec (offset %u) overlaps %s", + __func__, next, i ? "previous target" : "'struct dm_ioctl'"); + return -EINVAL; + } r = next_target(spec, next, end, &spec, &target_params); if (r) { @@ -1452,6 +1457,15 @@ static int populate_table(struct dm_table *table, return r; } + nul_terminator = memchr(target_params, 0, (size_t)(end - target_params)); + if (nul_terminator == NULL) { + DMERR("%s: target parameters not NUL-terminated", __func__); + return -EINVAL; + } + + /* Add 1 for NUL terminator */ + min_size = (size_t)(nul_terminator - (const char *)spec) + 1; + r = dm_table_add_target(table, spec->target_type, (sector_t) spec->sector_start, (sector_t) spec->length, -- Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab