Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2138350lqt; Mon, 22 Apr 2024 02:33:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW1DZs2lPz3IiAGoIQPsYA+Z+zVxgJ8MLvC++zm2gVNF5HskkXLGVEu6kM5/4FcPAJ3VCb6/uBudYoSbfAEfJDKSS7jjveeD2fm3qC8Ng== X-Google-Smtp-Source: AGHT+IHgpW02V8I2tVi3RBUgC+UrBnoyvJxm/i+ScsjXVZ6dtpugRy93zlHuXIVGM2TnQb6lvlpp X-Received: by 2002:a17:906:c34c:b0:a55:6d17:6fbf with SMTP id ci12-20020a170906c34c00b00a556d176fbfmr8265478ejb.5.1713778383361; Mon, 22 Apr 2024 02:33:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713778383; cv=pass; d=google.com; s=arc-20160816; b=kJMmtxmk7gj2ct7cxyvgScbihqIdsWfQR8DY2NVkzxmDIDFcVCzTMf6zpA4XwAtTJO WtsTx33Fp1P4JMFxhjYNlsXIZN267Oo0PRrVGmcKKRL7dY4DwMo28muex1ElUK+rCBka S/MyoDtxCbH2w0a5qOSwFhKvfl1OsvvHRSnR+G2udlOorWbdP5rfdkOugkrShKqSu+8r 0RMMyKPeqh0Iu7uHmRKxzCMENdbFzuF65MmB6M0qWw5oNBjGoe1Rxqw0Gxz5k5KYE8L8 zi2hUwfCbFO43tuiXFBWclvb4CZL5Ixl/dncBryDxrmfPzKy4MaKYzN7ESs1Ljj19Mqs ubjQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-disposition:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:subject:cc:to:from:date :dkim-signature; bh=S1CqQcJrzdvW3IqhkKP04Hdt5bWJd9zRL4G/OeUl8Pw=; fh=VljbuTJqCf1Sgzr8fVLo4WMm3TuBPH29A1YlGrxWwN4=; b=Zx55LTEIO5Vczf/7u3+qX5OqX9Q161elSDYl7n/o6aryOvA/zVHibUBj5a0TqnQ6rc 2PDS0yRulcfkkMNm2WWwSsuo6xmeN308zXbmx5NnXjUUJ7aFL2Fol1vXJiVcn6JsTQ84 g7AkKy/viuZjjbbkhRKmQ5lVzScEORAodZkgkwLDkDxEQo2/IylCzMKUpZvOnJlRvKpA oFy8vEXK1Y7IrzbL675RKLfFO3pbv+ZFGZ3X0EXX/HfhJo+ZoOrNKtAdtsu+v4PYAUve 0byYDKDda04Y7SJUSXH/nw0j4cHVWNmk74yGdtFVZ+uLdm4pMEo6OSWguM+TDvu7Nlgl n83A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jOp3yYHE; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-153092-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153092-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id wq3-20020a170907064300b00a5268a4f805si5394950ejb.901.2024.04.22.02.33.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 02:33:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-153092-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jOp3yYHE; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-153092-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153092-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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 am.mirrors.kernel.org (Postfix) with ESMTPS id DE8151F219CF for ; Mon, 22 Apr 2024 09:33:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9FE09626CB; Mon, 22 Apr 2024 09:32:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="jOp3yYHE" Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) (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 528C2C8F3 for ; Mon, 22 Apr 2024 09:32:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713778372; cv=none; b=XRkeMwOMPjEjMgZXGsRK4ompwSOBCk2F1We/kmic3JfCDFTZ9yA6j7/Vo/ZSoYg3v7JjV9QeV0ZEhzNazSE8jmwFqiRo9bMjMSSIBQyyj5R9WcaQanMnx/3M0IokmG4R7HXTy0NghQoYciOCUSUvd5cvBJp4cwCaW3eqQ1VHUDs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713778372; c=relaxed/simple; bh=ORQ71f0AaQQ5akKJiukuBgi17kny7qie7GaIGLxzN/I=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=SEwiWHJTC/1waMdGskfMqKIxYyp89RzCGX0xKGUnyF9YPh3kW0NW6bMVJWOquEeHI5zn64S0J2W86FtnyHJqDNYl6ddmb5SUOveowdZ74TjwzBPCZ5/mhfzveAX0MU3xkRbqkEcbWMk678wPx0U6HPXq1+Cmwfkh9qc8H6phBMU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=jOp3yYHE; arc=none smtp.client-ip=209.85.208.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-572040691c9so1553296a12.1 for ; Mon, 22 Apr 2024 02:32:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713778368; x=1714383168; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=S1CqQcJrzdvW3IqhkKP04Hdt5bWJd9zRL4G/OeUl8Pw=; b=jOp3yYHEkvhzxwN8/UjDO1vdZ5aXhFMHbjacqI+X033n5DCn6+XuLkC1J2TLci2ZON ghDYUv5T6mDEJteFGq5l2FGdKSqMzJHrA1o2bqAApx8CPyvayW6LE405FHnQD0rXc6MX 87wynayoeQAww1bBJ4bF1eDOHoZtHVzCOPQ+ZRBM2EeaOCI22Ej+JCf5Sw//pbExNV39 lCIqoiJgDpkC30LrRVTI/h9VJOW/HLaovzhfD28wqSvXdrmDBLc1g3kdVQceBkDvAMfx zlNi5u3uoieOM9/jUxXPu8rx+4KYSeE+aWAHQJD7RX7+3Dst/Wdwoceddbt4/sttfynB Em4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713778368; x=1714383168; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=S1CqQcJrzdvW3IqhkKP04Hdt5bWJd9zRL4G/OeUl8Pw=; b=IWe0Tgkn+EHGUqzbnVlZv0m+0ac8bg0ylogDaBB5bP+DO/EFDKnLMWdWItalKOKFxp AI/W66qZl+MNitempk3aQbtXYPEm+6eolLw8AwmNNg7rsIeHd2x9UCDBtdb18RbHUdId 3xrQnk0V2aU3cWJPRH1UWzJxjRAB/1QTsFUnA+cc9lJgbjrwHTY4HkICi36xqraZRJEi /4odaMcTZ+awbcCmTR/tZRjn4r2BSKbjyI3l8nH7FPoJsE932fbsln77JCAqSuTNcqHr BK7bYCCXz+rZHdf8ZzWQq04ZKd0Sw7O5Ufp0B+7tEpUpq+EI4lusurYCQMXLXDfjZy1p JPnw== X-Forwarded-Encrypted: i=1; AJvYcCXGckfZ7nyLQqfF8N2BowqOhJE8Cw5qrg4W0NyLuaIDJx0t0/4PPRIYqPoZUAR5LILevfkFyOWJ7lLOMTmzI4xvHZg4tGpVPQy+56qG X-Gm-Message-State: AOJu0YyqJf3HsYhnyuJgzU5oBvkDKbnNwxO6An9JADU+KEZNr/d1dq8q W49yE1PORBjfh7/bIMxFuWhpQ6havBnl8/lF2bmp8+558jY/h7gH4h/emS9RbUY= X-Received: by 2002:a50:99d3:0:b0:56b:dd7c:ac3d with SMTP id n19-20020a5099d3000000b0056bdd7cac3dmr8686797edb.10.1713778368390; Mon, 22 Apr 2024 02:32:48 -0700 (PDT) Received: from localhost ([102.222.70.76]) by smtp.gmail.com with ESMTPSA id g1-20020aa7c841000000b0056e718795f8sm5340030edt.36.2024.04.22.02.32.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 02:32:48 -0700 (PDT) Date: Mon, 22 Apr 2024 12:32:44 +0300 From: Dan Carpenter To: Ezequiel =?iso-8859-1?Q?Garc=EDa?= , Ghanshyam Agrawal Cc: Ezequiel Garcia , Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Ricardo Ribalda Subject: [PATCH v2] media: stk1160: fix bounds checking in stk1160_copy_video() Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Mailer: git-send-email haha only kidding The subtract in this condition is reversed. The ->length is the length of the buffer. The ->bytesused is how many bytes we have copied thus far. When the condition is reversed that means the result of the subtraction is always negative but since it's unsigned then the result is a very high positive value. That means the overflow check is never true. Additionally, the ->bytesused doesn't actually work for this purpose because we're not writing to "buf->mem + buf->bytesused". Instead, the math to calculate the destination where we are writing is a bit involved. You calculate the number of full lines already written, multiply by two, skip a line if necessary so that we start on an odd numbered line, and add the offset into the line. To fix this buffer overflow, just take the actual destination where we are writing, if the offset is already out of bounds print an error and return. Otherwise, write up to buf->length bytes. Fixes: 9cb2173e6ea8 ("[media] media: Add stk1160 new driver (easycap replacement)") Signed-off-by: Dan Carpenter --- v2: My first patch just reversed the if statement but that wasn't the correct fix. Ghanshyam Agrawal sent a patch last year to ratelimit the output from this function because it was spamming dmesg. This patch should hopefully fix the issue. Let me know if there are still problems. drivers/media/usb/stk1160/stk1160-video.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/drivers/media/usb/stk1160/stk1160-video.c b/drivers/media/usb/stk1160/stk1160-video.c index 366f0e4a5dc0..e79c45db60ab 100644 --- a/drivers/media/usb/stk1160/stk1160-video.c +++ b/drivers/media/usb/stk1160/stk1160-video.c @@ -99,7 +99,7 @@ void stk1160_buffer_done(struct stk1160 *dev) static inline void stk1160_copy_video(struct stk1160 *dev, u8 *src, int len) { - int linesdone, lineoff, lencopy; + int linesdone, lineoff, lencopy, offset; int bytesperline = dev->width * 2; struct stk1160_buffer *buf = dev->isoc_ctl.buf; u8 *dst = buf->mem; @@ -139,8 +139,13 @@ void stk1160_copy_video(struct stk1160 *dev, u8 *src, int len) * Check if we have enough space left in the buffer. * In that case, we force loop exit after copy. */ - if (lencopy > buf->bytesused - buf->length) { - lencopy = buf->bytesused - buf->length; + offset = dst - (u8 *)buf->mem; + if (offset > buf->length) { + dev_warn_ratelimited(dev->dev, "out of bounds offset\n"); + return; + } + if (lencopy > buf->length - offset) { + lencopy = buf->length - offset; remain = lencopy; } @@ -182,8 +187,13 @@ void stk1160_copy_video(struct stk1160 *dev, u8 *src, int len) * Check if we have enough space left in the buffer. * In that case, we force loop exit after copy. */ - if (lencopy > buf->bytesused - buf->length) { - lencopy = buf->bytesused - buf->length; + offset = dst - (u8 *)buf->mem; + if (offset > buf->length) { + dev_warn_ratelimited(dev->dev, "offset out of bounds\n"); + return; + } + if (lencopy > buf->length - offset) { + lencopy = buf->length - offset; remain = lencopy; } -- 2.43.0