Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2301438imm; Mon, 28 May 2018 05:41:48 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqgerYb6qtRmOEHrPBmHn/p3XMoiXaBR7wtNy8TFtqg8vtrVSRhoPxfmSHMP5atIdWjCRHZ X-Received: by 2002:a17:902:189:: with SMTP id b9-v6mr13639875plb.204.1527511308206; Mon, 28 May 2018 05:41:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527511308; cv=none; d=google.com; s=arc-20160816; b=q4M2c54edqi+tSWHVXE6eluTUz8q3U96JlI9GZljdRzfOPG0AwvCfaixTmDA2SzXLv H9lqVWPVxG61Kd8zpOOyIrH192iWZDAI6dgi/qUP2zJVVUHDl+SbQ95YUxthvE0Umgy6 ZEMr7iRFDE8FfAOyXIuDcqmvOLoRWYASPl4la8FtcDuKe1GGpQ9idpGQ6roK9Nxv8uq3 J7tlKDxHwi/CkYD+HjmKMOrmz54H45PXlFUGpmYsRbQf/q6CBYepiASKP3bienP+IDfw mT/7okNPPL7fwDKJaLGFbUXRYMu5v8nyg65HM/4xay8BNNIYQWL26Vho4/jhJvVjzcmQ oDBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=RcQsYr4zRrVe//lKXF4GmAuwVkkzbsEcvr35D1tWS1Q=; b=JTvPBwV6WC/5bahM3IMw0gpLAdog6SYYbFU/hpUGp00VeHVjDz6bak5hrp/9B2RM/d ffamhVxmUlsHW7U4FNRA8OVDMq8VDC3kS5+N+10/7WgpUF19baTBOSWcITlYjPguyFNK uaZa+nYNw+lZYdXqssjHYP7Yz0uRyXL4Z89xncwXUi8bxl3sYmTEh9wzU9bihLH3Fd2l 0qwBfceAZl5kl5rFN1x3u6Bl/hEJDmabWaePgjfLT8ct8zqOEBJNkhjqxY8TyteMTXF3 jToFbiAfI013MU5FIJOXo1c5sWITF3ztrEtmML2Zw2jeELTlkr3wOkLZrkNAkrE3RVge 07wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CaNR6SGO; 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 a68-v6si31449948pli.158.2018.05.28.05.41.33; Mon, 28 May 2018 05:41:48 -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=CaNR6SGO; 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 S937998AbeE1K6V (ORCPT + 99 others); Mon, 28 May 2018 06:58:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:46422 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S937983AbeE1K6R (ORCPT ); Mon, 28 May 2018 06:58:17 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (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 683182088E; Mon, 28 May 2018 10:58:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527505096; bh=E+s5wcYqcmMrfSvTVSwIcNiQqFmBjjiLW9f5MZyOZSU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CaNR6SGO/kWwT6lZKtalhZ4HSJHfa7oq1d9aBWg5pnIBjq2nb4BaeVCUZZY7CHc3R cqUQ2i0UnlkWzK/BkvKf+j0iHFCeeNjAsbc63P2GFEqLfGW9kppBQsD99XMtkCD8D8 6KkG0eZaHll/6mMoZnuXVu9U8Oh0zVzy4uDxcsK0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Frank Asseg , Jiri Kosina , Sasha Levin Subject: [PATCH 4.14 374/496] tools/thermal: tmon: fix for segfault Date: Mon, 28 May 2018 12:02:39 +0200 Message-Id: <20180528100335.572184987@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100319.498712256@linuxfoundation.org> References: <20180528100319.498712256@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Frank Asseg [ Upstream commit 6c59f64b7ecf2bccbe73931d7d573d66ed13b537 ] Fixes a segfault occurring when e.g. is pressed multiple times in the ncurses tmon application. The segfault is caused by incrementing cur_thermal_record in the main function without checking if it's value reached NR_THERMAL_RECORD immediately. Since the boundary check only occurred in update_thermal_data a race condition existed, which lead to an attempted read beyond the last element of the trec array. The fix was implemented by moving the cur_thermal_record incrementation to the update_thermal_data function using a temporary variable on which the boundary condition is checked before updating cur_thread_record, so that the variable is never incremented beyond the trec array's boundary. It seems the segfault does not occur on every machine: On a HP EliteBook G4 the segfault happens, while it does not happen on a Thinkpad T540p. Signed-off-by: Frank Asseg Signed-off-by: Jiri Kosina Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- tools/thermal/tmon/sysfs.c | 12 +++++++----- tools/thermal/tmon/tmon.c | 1 - 2 files changed, 7 insertions(+), 6 deletions(-) --- a/tools/thermal/tmon/sysfs.c +++ b/tools/thermal/tmon/sysfs.c @@ -486,6 +486,7 @@ int zone_instance_to_index(int zone_inst int update_thermal_data() { int i; + int next_thermal_record = cur_thermal_record + 1; char tz_name[256]; static unsigned long samples; @@ -495,9 +496,9 @@ int update_thermal_data() } /* circular buffer for keeping historic data */ - if (cur_thermal_record >= NR_THERMAL_RECORDS) - cur_thermal_record = 0; - gettimeofday(&trec[cur_thermal_record].tv, NULL); + if (next_thermal_record >= NR_THERMAL_RECORDS) + next_thermal_record = 0; + gettimeofday(&trec[next_thermal_record].tv, NULL); if (tmon_log) { fprintf(tmon_log, "%lu ", ++samples); fprintf(tmon_log, "%3.1f ", p_param.t_target); @@ -507,11 +508,12 @@ int update_thermal_data() snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, ptdata.tzi[i].instance); sysfs_get_ulong(tz_name, "temp", - &trec[cur_thermal_record].temp[i]); + &trec[next_thermal_record].temp[i]); if (tmon_log) fprintf(tmon_log, "%lu ", - trec[cur_thermal_record].temp[i]/1000); + trec[next_thermal_record].temp[i] / 1000); } + cur_thermal_record = next_thermal_record; for (i = 0; i < ptdata.nr_cooling_dev; i++) { char cdev_name[256]; unsigned long val; --- a/tools/thermal/tmon/tmon.c +++ b/tools/thermal/tmon/tmon.c @@ -336,7 +336,6 @@ int main(int argc, char **argv) show_data_w(); show_cooling_device(); } - cur_thermal_record++; time_elapsed += ticktime; controller_handler(trec[0].temp[target_tz_index] / 1000, &yk);