From patchwork Tue Jul 25 17:30:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Torvalds X-Patchwork-Id: 706885 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CC92C001DF for ; Tue, 25 Jul 2023 17:31:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232359AbjGYRbE (ORCPT ); Tue, 25 Jul 2023 13:31:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232266AbjGYRa4 (ORCPT ); Tue, 25 Jul 2023 13:30:56 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBD75E3 for ; Tue, 25 Jul 2023 10:30:53 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-99454855de1so849156566b.2 for ; Tue, 25 Jul 2023 10:30:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; t=1690306252; x=1690911052; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=8YN90U0y2k0M2aE2B56swcEJYHG48HYNScT8gjLzv1g=; b=OtCDhHkjbr+7s6r6dvvZ8tjowEwo7soFehYRLisESZnOmuab77UqJshxSb8CnBCGgy xD4Qxsj7DOCQo1BN30pQa7pQkNpKQm74CgtGcbf2L9FfHj7ni1LrGYiBGqUSBvr7w4AN 1jAhJequNF4wD2fRWBp6qKdZrrh5j8aJE8OWY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690306252; x=1690911052; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=8YN90U0y2k0M2aE2B56swcEJYHG48HYNScT8gjLzv1g=; b=HDaWzDlcNoDaD0qFTsZf/wZJBpm+REG9UcA3xw7KQ3yrwO/WxgRGi2hJJVGJ6kcVsp 5J/HzTmi9DwPvlizfDvK7wFeQK1JjvROy8BDA2oHTeFVq88Z1iAs/LVcYBFMunMayytA dBBokdPZBqe+8QTGrPcdsHHazil8QAkdmEEryEzK+bxwwuWvc1LwgyshX3sFdRKjf3py Df/To79SmikMd7ukazeNTOulGHuIU5e0Da0WfI3TqET/njNYaC2wtWXUYtp0ujmMU/zO dWHpuMWEkqgyXMgDBn2zhfL1yWpGGh+SJBlG4qDhQ07BV4LJBHJMaqldMtgd6a7+n117 BwUw== X-Gm-Message-State: ABy/qLYI9AJb1uH7Zakel39lJCJIpI2zhAZ4ckHiZ4gG0zbvLW524FQb XDd1C/kDba5ELmz8SYK+D0d5J09f6/wyaaZJWchpha/B X-Google-Smtp-Source: APBJJlFQDKUlQATPXYJZ5tIZUR8xLJ2JdnYT2rE4yACx82Foy95o65RmtZ38W5h52qlMQdir6FNH6A== X-Received: by 2002:a17:906:25e:b0:991:d8b2:ea31 with SMTP id 30-20020a170906025e00b00991d8b2ea31mr13543790ejl.52.1690306252204; Tue, 25 Jul 2023 10:30:52 -0700 (PDT) Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com. [209.85.208.45]) by smtp.gmail.com with ESMTPSA id jx21-20020a170906ca5500b00988a0765e29sm8428329ejb.104.2023.07.25.10.30.51 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 Jul 2023 10:30:51 -0700 (PDT) Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-51d95aed33aso8286327a12.3 for ; Tue, 25 Jul 2023 10:30:51 -0700 (PDT) X-Received: by 2002:a05:6402:2052:b0:522:3cf4:9d86 with SMTP id bc18-20020a056402205200b005223cf49d86mr3657239edb.33.1690306251162; Tue, 25 Jul 2023 10:30:51 -0700 (PDT) MIME-Version: 1.0 From: Linus Torvalds Date: Tue, 25 Jul 2023 10:30:34 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: SCSI: fix parsing of /proc/scsci/scsi file To: Martin K Petersen , James Bottomley , Tony Battersby , Willy Tarreau Cc: linux-scsi , Linux Kernel Mailing List Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org This is the simplified version of the fix proposed by Tony Battersby for the horrid scsi /proc parsing code. It doesn't make it prettier, it just makes it less buggy. That parsing code hasn't been changed in git or BK times, so it's at least two decades since anybody touched it, and judging by how nasty it is, it's probably more than that. This is v2 with the additional bug noted by Tony hopefully fixed. Linus >From 574fe269f5aaf62a3ec862bf430adf91a20823bd Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Tue, 25 Jul 2023 10:09:31 -0700 Subject: [PATCH] scsi: fix legacy /proc parsing buffer overflow The parsing code for /proc/scsi/scsi is disgusting and broken. We should have just used 'sscanf()' or something simple like that, but the logic may actually predate our kernel sscanf library routine for all I know. It certainly predates both git and BK histories. And we can't change it to be something sane like that now, because the string matching at the start is done case-insensitively, and the separator parsing between numbers isn't done at all, so *any* separator will work, including a possible terminating NUL character. This interface is root-only, and entirely for legacy use, so there is absolutely no point in trying to tighten up the parsing. Because any separator has traditionally worked, it's entirely possioble that people have used random characters rather than the suggested space. So don't bother to try to pretty it up, and let's just make a minimal patch that can be back-ported and we can forget about this whole sorry thing for another two decades. Just make it at least not traverse the terminating NUL. Reported-by: Tony Battersby Link: https://lore.kernel.org/linux-scsi/b570f5fe-cb7c-863a-6ed9-f6774c219b88@cybernetics.com/ Cc: Martin K Petersen Cc: James Bottomley Cc: Willy Tarreau Cc: stable@kernel.org Signed-off-by: Linus Torvalds --- drivers/scsi/scsi_proc.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c index 4a6eb1741be0..8aa8208ceb7f 100644 --- a/drivers/scsi/scsi_proc.c +++ b/drivers/scsi/scsi_proc.c @@ -383,6 +383,9 @@ static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun) return error; } +/* increment 'p', but not past the end */ +static inline char *next_p(char *p) { return p + !!*p; } + /** * proc_scsi_write - handle writes to /proc/scsi/scsi * @file: not used @@ -431,12 +434,12 @@ static ssize_t proc_scsi_write(struct file *file, const char __user *buf, * with "0 1 2 3" replaced by your "Host Channel Id Lun". */ if (!strncmp("scsi add-single-device", buffer, 22)) { - p = buffer + 23; + p = buffer + 22; - host = simple_strtoul(p, &p, 0); - channel = simple_strtoul(p + 1, &p, 0); - id = simple_strtoul(p + 1, &p, 0); - lun = simple_strtoul(p + 1, &p, 0); + host = simple_strtoul(next_p(p), &p, 0); + channel = simple_strtoul(next_p(p), &p, 0); + id = simple_strtoul(next_p(p), &p, 0); + lun = simple_strtoul(next_p(p), &p, 0); err = scsi_add_single_device(host, channel, id, lun); @@ -445,12 +448,12 @@ static ssize_t proc_scsi_write(struct file *file, const char __user *buf, * with "0 1 2 3" replaced by your "Host Channel Id Lun". */ } else if (!strncmp("scsi remove-single-device", buffer, 25)) { - p = buffer + 26; + p = buffer + 25; - host = simple_strtoul(p, &p, 0); - channel = simple_strtoul(p + 1, &p, 0); - id = simple_strtoul(p + 1, &p, 0); - lun = simple_strtoul(p + 1, &p, 0); + host = simple_strtoul(next_p(p), &p, 0); + channel = simple_strtoul(next_p(p), &p, 0); + id = simple_strtoul(next_p(p), &p, 0); + lun = simple_strtoul(next_p(p), &p, 0); err = scsi_remove_single_device(host, channel, id, lun); } -- 2.41.0.327.gaa9166bcc0