Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3472294pxj; Sun, 20 Jun 2021 22:26:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5C7ZuZ6XKOTDc1ZIfpbKDlPXWiyGSC/H/8cdQ/99U/G2ivI3hH0bqYf5eLQDAfqJOknhy X-Received: by 2002:a17:907:d0d:: with SMTP id gn13mr9232222ejc.199.1624253193741; Sun, 20 Jun 2021 22:26:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624253193; cv=none; d=google.com; s=arc-20160816; b=F0c1/tTwAo8PsIjt9gVbyPRR2fi8hPH5AVj1+nI7NFgBVJs4erM2NxnOM7alLWKctL 2/2afu/CSpmFpT96I8CpVwgQNM4orw8sMGh+mRMECweZsXO8n60FLXGBNXErNSYsZ7jZ b18RG31AwTMVjCgMDP0rhTeZ+WAeu1YrQ2mGut3eT2+JJcPzKZrLZTKGpfvsFzeaGUOx ubmgSyNcKl8MGyMgz+tXVgMlD4dSEAPoQLwWqatYCwWgDBfL38XdiXRtI1JX18A4qfJb 97ZZkEYi+F+1XDwFQZq5OqyZBTCWlABwHGW+s+WwW+FPKKOnVZ6L5ETmh8jSY1+I3Uxx W+mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=1qVAoYNk5ZopylsEj2qCzbf543VF7z7ZlB5UYvXcDuI=; b=u5h9+gSXaGAd/XMWWmYmpu0K2yUhdzlXR9LAgo8fg9jCOGsU5VgwomyX5MvPO6dQxD Y6RYiYmcYVgJ8Dz/OdbmTQdf+v/OLFYXMFZdCgvSUr9hf5/0pMjm+Cn5noGK23tNraAN eC5N+T7V8lK/4K44YevRqd51iM/AMrSYAT+23iu+Ib5wKJC0LXrYjsI4PG359wcn8dbf V7JOy4tAQbIvzmXVFAJpno3HZETdyzn5N4uPSSwdXHCVtUBLVaVU7+dlgZ0r9nmNIepa BBMpQsj0MgXpwj8Ix5jhQhbH/yDs/sO7r6X0x6T2zb9hLnXVGA52UA49wUs10QGbmC57 +EZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=o8cXAWOY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n9si6917555ejl.335.2021.06.20.22.26.11; Sun, 20 Jun 2021 22:26:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=o8cXAWOY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229597AbhFUF12 (ORCPT + 99 others); Mon, 21 Jun 2021 01:27:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229486AbhFUF11 (ORCPT ); Mon, 21 Jun 2021 01:27:27 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6EF8C061574; Sun, 20 Jun 2021 22:25:12 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id t19-20020a17090ae513b029016f66a73701so4510053pjy.3; Sun, 20 Jun 2021 22:25:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=1qVAoYNk5ZopylsEj2qCzbf543VF7z7ZlB5UYvXcDuI=; b=o8cXAWOYVtQerhlY9qg+MFlJo6FPNgDQscSbfLfanhgNNooHFb1DOPVyw9XR3LCRPq dFKL4xFyDp6SUGi/7EcgmyHWs45UEVR0VJQd/PnM+K5AwVDOc5Bd1/i3QKVy4uQjxugO QDY45KOfVk+ySEbwdtJSdjXNC2v7cJT7QgMoi3w9/03MHePlzt22jT/I1MfcD6n7jlch Ei7QEEUrl1XJO3I/Hk2GfxRXlNygIYlEHqoW6qH/5vM5FEIUfwMo5PKrf80c4wtzsQEu vjU95k9JBjWiMpT5tMfdhXMKlne6HdPlkpPxpPKYFTeChbfB/ZHRQfPdJCOBYRYBm3jb 4JVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=1qVAoYNk5ZopylsEj2qCzbf543VF7z7ZlB5UYvXcDuI=; b=UeZ3we64/69R03o3pFX43YQQoU+88hrVaXKHCbeDyIsBGyPs4Wkmxoiex0uCz2hiyB deavNBR+zurtU5nFK9Mgec3ZADGtYODY24RyCbD/cPxHho2uN9GlkORkqbwyzdYhjskQ BOcdPOU4BPSFuQguPdfu76UlLdGb8QeNhcsGgbn9XqbSFbpTKREJwY56cXZeA++jB2N6 ivXBsRG/cNFV7E6AgdzNF9wlFRxLsRaDgKT8LE4VV1v+8dOswlvzzzIMyexTY6jxaCIV mlo2Ae/GlzpYXn1mJBVJczuPjNVG6RUdGuhj5XdaVZ+HEpQ/11WW8KKd70hCeb1Dp1Nq utWA== X-Gm-Message-State: AOAM532fu+vK/BWBAramedvpkooweTIygn+bh3P4xRnrWKT6Gq+32FDw jvgT+fZHb7lQ4FVmcWQpNl2csDO4yAM= X-Received: by 2002:a17:902:d909:b029:11b:870f:ddad with SMTP id c9-20020a170902d909b029011b870fddadmr16374023plz.81.1624253112211; Sun, 20 Jun 2021 22:25:12 -0700 (PDT) Received: from google.com ([2620:15c:202:201:df5a:b40e:40c9:a3e7]) by smtp.gmail.com with ESMTPSA id w123sm13835706pfb.109.2021.06.20.22.25.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Jun 2021 22:25:11 -0700 (PDT) Date: Sun, 20 Jun 2021 22:25:08 -0700 From: Dmitry Torokhov To: Linus Torvalds Cc: Alexander Larkin , Dan Carpenter , linux-input@vger.kernel.org, lkml , Security Officers , Murray McAllister Subject: Re: [PATCH] Input: joydev - prevent potential write out of bounds in ioctl Message-ID: References: <20210620120030.1513655-1-avlarkin82@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jun 20, 2021 at 09:37:47AM -0700, Linus Torvalds wrote: > On Sun, Jun 20, 2021 at 5:01 AM Alexander Larkin wrote: > > > > The problem is that the check of user input values that is just > > before the fixed line of code is for the part of first values > > (before len or before len/2), but then the usage of all the values > > including i >= len (or i >= len/2) could be. > > No, I think the problem is simpler than that. > > > - for (i = 0; i < joydev->nabs; i++) > > + for (i = 0; i < len && i < joydev->nabs; i++) > > joydev->absmap[joydev->abspam[i]] = i; > > This part is unnecessary - all values of "joydev->abspam[i]" have been > validated (either they are the old ones, or the new ones that we just > validated). > > > memcpy(joydev->keypam, keypam, len); > > > > - for (i = 0; i < joydev->nkey; i++) > > + for (i = 0; i < (len / 2) && i < joydev->nkey; i++) > > joydev->keymap[keypam[i] - BTN_MISC] = i; > > The problem here is not that we walk past "len/2", but that the code > *should* have used > > joydev->keymap[joydev->keypam[i] - BTN_MISC] = i; > > (note the "keypam[1]" vs "joydev->keypam[i]"). > > And the reason it *should* walk the whole "joydev->nkey" is that if > there are later cases with the same keypam value, the later ones > should override the previous ones (well, that "should" is more a > "traditionally have"). Yes, we can discuss whether "short" ioctl should clear out the part of map that is not supplied by the call, but given that I consider joydev legacy my preference would be to leave this as it was. > > So I think the right patch is this one-liner > > diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c > index da8963a9f044..947d440a3be6 100644 > --- a/drivers/input/joydev.c > +++ b/drivers/input/joydev.c > @@ -499,7 +499,7 @@ static int joydev_handle_JSIOCSBTNMAP(struct > joydev *joydev, > memcpy(joydev->keypam, keypam, len); > > for (i = 0; i < joydev->nkey; i++) > - joydev->keymap[keypam[i] - BTN_MISC] = i; > + joydev->keymap[joydev->keypam[i] - BTN_MISC] = i; > > out: > kfree(keypam); > > (whitespace-damaged, I would like Dmitry to think about it rather than > apply this mindlessly. > > Dmitry? Yes, this makes sense to me and it is safe as joydev->keypam is guaranteed to be the right size. Are you going to reformat this and resend or should I? Thanks. -- Dmitry