Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEAABC43381 for ; Mon, 1 Apr 2019 20:54:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7A4CC2133D for ; Mon, 1 Apr 2019 20:54:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="au/X6UHK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726900AbfDAUyP (ORCPT ); Mon, 1 Apr 2019 16:54:15 -0400 Received: from mail-pg1-f202.google.com ([209.85.215.202]:45766 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726725AbfDAUyO (ORCPT ); Mon, 1 Apr 2019 16:54:14 -0400 Received: by mail-pg1-f202.google.com with SMTP id f1so8267222pgv.12 for ; Mon, 01 Apr 2019 13:54:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=8Psqf/6t8dGynHuTRWd5GZjH/s0RxTMoQNufq/VXlEw=; b=au/X6UHKK61PoAGS++G3OvaPZatR9X9pIivFy0c2IoQG+JVIATu8hUNF3cJjaNXZHO mjgVnincsz9ll7tpKG5dahwBFsJYyi3qn6PsD2Yq/2yh/5VW2S/6GcM/hiHkqcxv1GsD EMRsRdBUl/6JOZlQOItYVA4R4hAB6gIqO8F4t11gUQb8RdTJNljh+CUedmp2iU1tGe7q b5UJ3AKUkxUDumJWDXq6IKQiBZ2BTVDl5Rx43BPHqLtgEnFP0IXbZHKHZe5mj5unUc57 xpMqCwsFnAmn0FBHkPqU+YQXMFXACNePKcBkUO4O+lfnRHW8x5ayA6WSbhrLUKfRDZYh CV6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=8Psqf/6t8dGynHuTRWd5GZjH/s0RxTMoQNufq/VXlEw=; b=YoGU8yJz+SBJHnQvlUyjXouPhxsTu8bDBFsc1LAsbzwHVIQP40fLG7w/lASSwqB10e VLh3xabP0DXLQW69NRlhp6LK+5dOBsyaCMYGH1i3WvjEqYiLaQiO3Lgm/A19D/RsZ7p3 AhsFO9SfxWPn7UPb9QHuRsh/yWeQAMDa2OcovayH2gErguLQksqSRvw5F1BfQU7MW47q Wjkhf0/HEnuhS6UG5zDvpStsnAsn0g+fVxQHrvX0Bu3AFn2o55tfcQly04CqRM9wDutd zyH3W3BypiwIOFk0TWRhGglEJqL5bNBsdaJ1uI05QB20CgfjobwzEkEfiNx9NDhgkY+q tijA== X-Gm-Message-State: APjAAAWWj1s2p98BFEYB/oXOtdX+8rNVTsRyRK0yG5ALiIhuhiv3HzS8 IsGrj/cf2b4WeFontrJCyNg4taJ0QPEFiDScWjC7tiZ5AgmeVOyjnci0GR1BcmGJ58cv9xzpyOR hpnurh05s/gtcScNMSDVDPUvmlG5GLzT/ysne5SVhPw1U5J71PJ+FEhHmuVIQ3zCt X-Google-Smtp-Source: APXvYqzztLfrVWZtTDgr1TfvuyTjRgX5YdpizMHuNx5wqIeJPzcGZjzkNopHsBPEfHM/BK2uZeuwmVbk6B4f X-Received: by 2002:a17:902:5c4:: with SMTP id f62mr839677plf.39.1554152053781; Mon, 01 Apr 2019 13:54:13 -0700 (PDT) Date: Mon, 1 Apr 2019 13:54:04 -0700 Message-Id: <20190401205404.63847-1-connoro@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog Subject: [PATCH] i2c: core-smbus: prevent stack corruption on read I2C_BLOCK_DATA From: "Connor O'Brien" To: stable@vger.kernel.org Cc: Jeremy Compostella , Wolfram Sang , stable@kernel.org, "Connor O'Brien" Content-Type: text/plain; charset="UTF-8" Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Jeremy Compostella commit 89c6efa61f5709327ecfa24bff18e57a4e80c7fa upstream. On a I2C_SMBUS_I2C_BLOCK_DATA read request, if data->block[0] is greater than I2C_SMBUS_BLOCK_MAX + 1, the underlying I2C driver writes data out of the msgbuf1 array boundary. It is possible from a user application to run into that issue by calling the I2C_SMBUS ioctl with data.block[0] greater than I2C_SMBUS_BLOCK_MAX + 1. This patch makes the code compliant with Documentation/i2c/dev-interface by raising an error when the requested size is larger than 32 bytes. Call Trace: [] dump_stack+0x67/0x92 [] panic+0xc5/0x1eb [] ? vprintk_default+0x1f/0x30 [] ? i2cdev_ioctl_smbus+0x303/0x320 [] __stack_chk_fail+0x1b/0x20 [] i2cdev_ioctl_smbus+0x303/0x320 [] i2cdev_ioctl+0x4d/0x1e0 [] do_vfs_ioctl+0x2ba/0x490 [] ? security_file_ioctl+0x43/0x60 [] SyS_ioctl+0x79/0x90 [] entry_SYSCALL_64_fastpath+0x12/0x6a Signed-off-by: Jeremy Compostella Signed-off-by: Wolfram Sang Cc: stable@kernel.org [connoro@google.com: 3.18 backport: adjust filename] Signed-off-by: Connor O'Brien --- drivers/i2c/i2c-core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 7bd1b5cfb8d1..bb4b76f44fc9 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -2762,16 +2762,16 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, the underlying bus driver */ break; case I2C_SMBUS_I2C_BLOCK_DATA: + if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { + dev_err(&adapter->dev, "Invalid block %s size %d\n", + read_write == I2C_SMBUS_READ ? "read" : "write", + data->block[0]); + return -EINVAL; + } if (read_write == I2C_SMBUS_READ) { msg[1].len = data->block[0]; } else { msg[0].len = data->block[0] + 1; - if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { - dev_err(&adapter->dev, - "Invalid block write size %d\n", - data->block[0]); - return -EINVAL; - } for (i = 1; i <= data->block[0]; i++) msgbuf0[i] = data->block[i]; } -- 2.21.0.392.gf8f6787159e-goog