Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3703998imu; Mon, 28 Jan 2019 09:19:09 -0800 (PST) X-Google-Smtp-Source: ALg8bN50w63sQ/h93ZcUOhsx/Bn9yRr2VmAsetDDJ4+W8KUj8bIrYqQIXHWUMkU2Jx+YgA03mM1y X-Received: by 2002:a63:4e15:: with SMTP id c21mr20614546pgb.50.1548695949646; Mon, 28 Jan 2019 09:19:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548695949; cv=none; d=google.com; s=arc-20160816; b=ScI/F+tHyKVxThU4B+5YXQ0p1oP7L7xpuNjj1QercTDjCykBi+MXLQl+urXMnhU0MF QKGHCqIyxn3VJmPMy7Ix+OWjrQr209bWa0ByC1KL7B4rj4aMuaVqHHjPi/2hZjp+o+v8 Eh7ufGtEWk9Y/1PS3OVQkUCVQbExZZJxG/DgDJl9DxnHdvIG2vMlypss43rogVZzVN3i PbMAFivzs7u8wbP+R1Uk8pmMVc+BikHOJ+goxe54QEKDKzO3wsj+CdhWC1rx9PYWtOrG /sAKXufKZ8FtLKJz0L/P4+Ik7pBC3BLir/EIZtLHIWJAT/nyFfIaHFdHnywZjRdTfF95 2OGQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=j2L0MiogvJPIRuTF2kCMfA7gvVpMrO4r4axMrD+Ff38=; b=sDuq6dv3w8g/KErhzWIlOLuxYVopMOQIDVJskEPceIZeW3U1LmC7zvHhGEgChzhAE8 iCJig/4p4fD2JxVl/VkFG8vVh5Ilh8aAK2AS/Ji6gDTWLMECVMUyS6YFQsYhxIrBjawA lus7oXD87OidD57IooLxt1De8+aX0aKsVJ/4h9FG+DqCA4KNg3vKYL691E7ty3gq2XbY WTguWER606bq3hAVkDHOFJBL6PqreLdGZ5lmA2fVuJVPcIRSTrmYxT9Pk/3mih+EmgRl 6zZ8smSsEYpKW/rroloUzTQz0eWrn7CmVBotESijboeoMinuHdL2TZLBKsGGwHJ2nwc/ yZtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zjuUOu68; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n125si7115238pga.179.2019.01.28.09.18.54; Mon, 28 Jan 2019 09:19:09 -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=zjuUOu68; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731816AbfA1QFZ (ORCPT + 99 others); Mon, 28 Jan 2019 11:05:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:51546 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731142AbfA1QFN (ORCPT ); Mon, 28 Jan 2019 11:05:13 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 59C2E21738; Mon, 28 Jan 2019 16:05:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548691512; bh=MZHO3LWVej1wSgyOn8Ul32wF6rSSTEgIMU++erNEBXQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zjuUOu684ui9adCCvuEOnMCpr9xtacR9v4xG963GB70BE7u6qWq5OqF7nDDke+r6w njklLlGtUlXgUdgOuXxiz5uxIgESR4HXfG6Rsy9R/fFo8q5J0XZaAXXMPIPBcTnWKC kYXX+shaynbXIHiAKWF1196zyNf4Qp8CdDHiAS9g= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Dan Carpenter , Steven Rostedt , Sasha Levin Subject: [PATCH AUTOSEL 4.19 123/258] tracing: Have trace_stack nr_entries compare not be so subtle Date: Mon, 28 Jan 2019 10:57:09 -0500 Message-Id: <20190128155924.51521-123-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190128155924.51521-1-sashal@kernel.org> References: <20190128155924.51521-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dan Carpenter [ Upstream commit ca16b0fbb05242f18da9d810c07d3882ffed831c ] Dan Carpenter reviewed the trace_stack.c code and figured he found an off by one bug. "From reviewing the code, it seems possible for stack_trace_max.nr_entries to be set to .max_entries and in that case we would be reading one element beyond the end of the stack_dump_trace[] array. If it's not set to .max_entries then the bug doesn't affect runtime." Although it looks to be the case, it is not. Because we have: static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] = { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX }; struct stack_trace stack_trace_max = { .max_entries = STACK_TRACE_ENTRIES - 1, .entries = &stack_dump_trace[0], }; And: stack_trace_max.nr_entries = x; for (; x < i; x++) stack_dump_trace[x] = ULONG_MAX; Even if nr_entries equals max_entries, indexing with it into the stack_dump_trace[] array will not overflow the array. But if it is the case, the second part of the conditional that tests stack_dump_trace[nr_entries] to ULONG_MAX will always be true. By applying Dan's patch, it removes the subtle aspect of it and makes the if conditional slightly more efficient. Link: http://lkml.kernel.org/r/20180620110758.crunhd5bfep7zuiz@kili.mountain Signed-off-by: Dan Carpenter Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Sasha Levin --- kernel/trace/trace_stack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/trace/trace_stack.c b/kernel/trace/trace_stack.c index 4237eba4ef20..6e3edd745c68 100644 --- a/kernel/trace/trace_stack.c +++ b/kernel/trace/trace_stack.c @@ -286,7 +286,7 @@ __next(struct seq_file *m, loff_t *pos) { long n = *pos - 1; - if (n > stack_trace_max.nr_entries || stack_dump_trace[n] == ULONG_MAX) + if (n >= stack_trace_max.nr_entries || stack_dump_trace[n] == ULONG_MAX) return NULL; m->private = (void *)n; -- 2.19.1