Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp2752652ybh; Mon, 5 Aug 2019 06:18:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqyPfKoYLW7OgM0sT30Umbkk1NqbiilcrQoHTAxr6lPK1H6flDOrwbJb9RmeCYx+oKHidpbA X-Received: by 2002:a63:1d0e:: with SMTP id d14mr136502821pgd.324.1565011087965; Mon, 05 Aug 2019 06:18:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565011087; cv=none; d=google.com; s=arc-20160816; b=jEMT2FnYE2Xpye48vMONjGfQPXUnOAl3LqhXJQTsnvZLshY6yksBs5aHbBghYzuLcN M6NlZ2JrTiUYjwO00NLlYWiTlFFgQbqmtHe9L2WmSsHkQgUSgh+AcyZWtYjm6d4p7e+f cvQUY9ZiI6Fq+HDvnL6EsZfiOFQudqRrQxfmWjfi/RDELdfsY7MbeoWhZiD7xV2Y8lOo kgHtkbVIYM1AY0NLAOCcAZAzM9lLZd74GF/5NGKc+Fai3QsjJZ8e8lhobyH3s7HnnMKH MNxoRukNnqqcNqHZiLky5s222ZkjQ8Ddqjlwwdbhu6iKVF8+Nj8Sy6TzKPRPQ3UV3IU9 B2Jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5EXVSua6ztWR4myhjo8zCIi5J8ETeZGuQ1IUG0b0jyM=; b=0C9W4o7kqAqQYA+uNnaDDfnQMes7I9MRsT3KWurqIfG/s5u9vjCJWpA8Niagdcck1d jtUu1tFXVJRF6/0b97YFLsk76BkvCfLMucBWcdKBg5NIsVn5TzCuSmg+QGkMpnAWhyQW rLogFTyjgj31nlrHlk4P9zaGtdGopaJR0hbDuhgnwCbkOG/0Siq0naiH8Jkr1Ib04WXP nsRIh9x4V96CP8lWgC1KzcYLINMikAidVmqi4TKWJV2HweWXkvXxpCAOea6fKj2U1927 gxnDRgxZNZ0VjrKCv2A9VNwkndzPyAA1ZxbXWoFk4AF5mR8sOdKaBkAmN+g9OS7ZNl+B WoHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YPDs3YsL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z193si43341660pgd.235.2019.08.05.06.17.53; Mon, 05 Aug 2019 06:18:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YPDs3YsL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728826AbfHENFb (ORCPT + 99 others); Mon, 5 Aug 2019 09:05:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:41488 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729296AbfHENF0 (ORCPT ); Mon, 5 Aug 2019 09:05:26 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DDB01216B7; Mon, 5 Aug 2019 13:05:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565010325; bh=w121SKm28VtyLycjqIt6tSeDVk3l9HfcjiPticrwHh4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YPDs3YsLVGxxsiFd2peXNylqKDYkOWIjV0mLbP5ZLJyezfe8Yd26tvk3h6+I1Fla9 TK4XeWGgBa9i2bKoZ2Et3QDi7/puGADCzCcQF1YzdJSk+e2MH9H6eNhDdmHLfhgJy9 nhVqHqRp3pVPj5f7hmV5btviBw4i0CbB77cE8jyI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kees Cook , Andreas Christoforou , "Eric W. Biederman" , Al Viro , Arnd Bergmann , Davidlohr Bueso , Manfred Spraul , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.9 23/42] ipc/mqueue.c: only perform resource calculation if user valid Date: Mon, 5 Aug 2019 15:02:49 +0200 Message-Id: <20190805124927.673910632@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190805124924.788666484@linuxfoundation.org> References: <20190805124924.788666484@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit a318f12ed8843cfac53198390c74a565c632f417 ] Andreas Christoforou reported: UBSAN: Undefined behaviour in ipc/mqueue.c:414:49 signed integer overflow: 9 * 2305843009213693951 cannot be represented in type 'long int' ... Call Trace: mqueue_evict_inode+0x8e7/0xa10 ipc/mqueue.c:414 evict+0x472/0x8c0 fs/inode.c:558 iput_final fs/inode.c:1547 [inline] iput+0x51d/0x8c0 fs/inode.c:1573 mqueue_get_inode+0x8eb/0x1070 ipc/mqueue.c:320 mqueue_create_attr+0x198/0x440 ipc/mqueue.c:459 vfs_mkobj+0x39e/0x580 fs/namei.c:2892 prepare_open ipc/mqueue.c:731 [inline] do_mq_open+0x6da/0x8e0 ipc/mqueue.c:771 Which could be triggered by: struct mq_attr attr = { .mq_flags = 0, .mq_maxmsg = 9, .mq_msgsize = 0x1fffffffffffffff, .mq_curmsgs = 0, }; if (mq_open("/testing", 0x40, 3, &attr) == (mqd_t) -1) perror("mq_open"); mqueue_get_inode() was correctly rejecting the giant mq_msgsize, and preparing to return -EINVAL. During the cleanup, it calls mqueue_evict_inode() which performed resource usage tracking math for updating "user", before checking if there was a valid "user" at all (which would indicate that the calculations would be sane). Instead, delay this check to after seeing a valid "user". The overflow was real, but the results went unused, so while the flaw is harmless, it's noisy for kernel fuzzers, so just fix it by moving the calculation under the non-NULL "user" where it actually gets used. Link: http://lkml.kernel.org/r/201906072207.ECB65450@keescook Signed-off-by: Kees Cook Reported-by: Andreas Christoforou Acked-by: "Eric W. Biederman" Cc: Al Viro Cc: Arnd Bergmann Cc: Davidlohr Bueso Cc: Manfred Spraul Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- ipc/mqueue.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ipc/mqueue.c b/ipc/mqueue.c index d5491a8807515..3f7dc5f341f7e 100644 --- a/ipc/mqueue.c +++ b/ipc/mqueue.c @@ -369,7 +369,6 @@ static void mqueue_evict_inode(struct inode *inode) { struct mqueue_inode_info *info; struct user_struct *user; - unsigned long mq_bytes, mq_treesize; struct ipc_namespace *ipc_ns; struct msg_msg *msg, *nmsg; LIST_HEAD(tmp_msg); @@ -392,16 +391,18 @@ static void mqueue_evict_inode(struct inode *inode) free_msg(msg); } - /* Total amount of bytes accounted for the mqueue */ - mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) + - min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) * - sizeof(struct posix_msg_tree_node); - - mq_bytes = mq_treesize + (info->attr.mq_maxmsg * - info->attr.mq_msgsize); - user = info->user; if (user) { + unsigned long mq_bytes, mq_treesize; + + /* Total amount of bytes accounted for the mqueue */ + mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) + + min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) * + sizeof(struct posix_msg_tree_node); + + mq_bytes = mq_treesize + (info->attr.mq_maxmsg * + info->attr.mq_msgsize); + spin_lock(&mq_lock); user->mq_bytes -= mq_bytes; /* -- 2.20.1