Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4821387ybl; Wed, 22 Jan 2020 05:23:44 -0800 (PST) X-Google-Smtp-Source: APXvYqwYGsQLF3bq99NmazykYP8IAVFdLRq5THEMVt+GSWP87MGpDMEAKkZv4gIND74FyRwSUVEE X-Received: by 2002:a9d:4d81:: with SMTP id u1mr7512170otk.323.1579699424575; Wed, 22 Jan 2020 05:23:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579699424; cv=none; d=google.com; s=arc-20160816; b=k38rzzb6j6yOccKbO3bbb/oe4C/DAZmyWwgYewj9iedUSu+fJn+Fo1kq45Y9sPO8gB 0MCz0Qx3Yb/Bp2B1iflXjSROtGYokBuI1WQuyZcBGt/KjPh0V0NclTIb6aFZuwN/cQIU AXL71+Xe7rTudmgjQTwd7MZ1H4Pxf6hnuMQW6nmgHX6768n5gPOp+lLH5AJLrs/npG8L 7IH9aiEcHRc7KGsNkFt+fbqe/2ZXEbQM/Axz6xYTALigI3QcGBVvxipQcCrLlSpuJcmj MVT5euW/NkdH6GdcKPADNgIS33qodZQCcYeP4Xh/xoidyV4iSfqOQU4HKd6ppgpOAB+0 tw/A== 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=WVpxO+ReQrM5wvg/ukt2ZPw2fVf4Ds7Ztgf9+D5jb0Y=; b=G+r0byVOucymZnA7+8W9PXnFHJSP4dFcPu8dB8pA9jdQRPqtqBDlvd9zfEeua1JT8+ UAFvxa413qknpndv/L91Ke3rCTLf9A7M0Gl5TjK/18xhjf+93b/qtCXi+bSTKd7ukNoo +9Cgh7joDCp1TXZbpQ6liRn5aMjR4psovtVPDUt4rAUMGu8XFpkKtZZ6aSywqppTm3QB ZcDzxwDAnCGSLRN+MEmo7nJUV5b/Rq9Tp6QRMPt4oPZ56xGAmiIRZTo6l8mlAs2qQt5W O1ARYkwenLd+X5+kLDReoSauZ6QUOn1+ZRlZrh2YzXbHmXzMmCHed7dSwGfjGdoIQjn4 sO9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nyh73MDp; 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 u12si23237808otq.51.2020.01.22.05.23.32; Wed, 22 Jan 2020 05:23:44 -0800 (PST) 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=nyh73MDp; 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 S1730493AbgAVNWQ (ORCPT + 99 others); Wed, 22 Jan 2020 08:22:16 -0500 Received: from mail.kernel.org ([198.145.29.99]:39664 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730468AbgAVNWN (ORCPT ); Wed, 22 Jan 2020 08:22:13 -0500 Received: from localhost (unknown [84.241.205.26]) (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 2CC20205F4; Wed, 22 Jan 2020 13:22:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579699332; bh=7BagCFkr7Ej8uB6WoRDmtl/Nn2vddSkOeXvraWty/M4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nyh73MDpmwXOcaf3EKrLBQO68fBBGYXY2sAWwanEmpnkGGxK83g2NFwlirMjnNnFQ tepMCbs0E3pficQj3d4PQYuF9ii2XQenZlsewhrD8SH4mlZjkl6jkJYFdKFoXPDcuR 4hAg4ipJ2SH3musOMsQCdwdYh7Mz7wV7bOLEFDts= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Waiman Long , "Peter Zijlstra (Intel)" , Bart Van Assche , Linus Torvalds , Thomas Gleixner , Ingo Molnar Subject: [PATCH 5.4 076/222] locking/lockdep: Fix buffer overrun problem in stack_trace[] Date: Wed, 22 Jan 2020 10:27:42 +0100 Message-Id: <20200122092839.158437396@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122092833.339495161@linuxfoundation.org> References: <20200122092833.339495161@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 From: Waiman Long commit d91f3057263ceb691ef527e71b41a56b17f6c869 upstream. If the lockdep code is really running out of the stack_trace entries, it is likely that buffer overrun can happen and the data immediately after stack_trace[] will be corrupted. If there is less than LOCK_TRACE_SIZE_IN_LONGS entries left before the call to save_trace(), the max_entries computation will leave it with a very large positive number because of its unsigned nature. The subsequent call to stack_trace_save() will then corrupt the data after stack_trace[]. Fix that by changing max_entries to a signed integer and check for negative value before calling stack_trace_save(). Signed-off-by: Waiman Long Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Bart Van Assche Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Fixes: 12593b7467f9 ("locking/lockdep: Reduce space occupied by stack traces") Link: https://lkml.kernel.org/r/20191220135128.14876-1-longman@redhat.com Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- kernel/locking/lockdep.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -482,7 +482,7 @@ static struct lock_trace *save_trace(voi struct lock_trace *trace, *t2; struct hlist_head *hash_head; u32 hash; - unsigned int max_entries; + int max_entries; BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE); BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES); @@ -490,10 +490,8 @@ static struct lock_trace *save_trace(voi trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries); max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries - LOCK_TRACE_SIZE_IN_LONGS; - trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3); - if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES - - LOCK_TRACE_SIZE_IN_LONGS - 1) { + if (max_entries <= 0) { if (!debug_locks_off_graph_unlock()) return NULL; @@ -502,6 +500,7 @@ static struct lock_trace *save_trace(voi return NULL; } + trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3); hash = jhash(trace->entries, trace->nr_entries * sizeof(trace->entries[0]), 0);