Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp329601ybv; Thu, 6 Feb 2020 23:58:23 -0800 (PST) X-Google-Smtp-Source: APXvYqyjj5fiLBLZAJyIBPR1RzXbSv79NeUejeecKBRPOSLCMsJQVnIDECfI9Vape5XeF4FHkApt X-Received: by 2002:a9d:394:: with SMTP id f20mr1752621otf.148.1581062303711; Thu, 06 Feb 2020 23:58:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581062303; cv=none; d=google.com; s=arc-20160816; b=U87FN5wlgBHoppU/Wf4aWDk8CVxZ23KmXAOE2Eb29qHLSlL3zoRiYz6exquau7jUoc rCrYwkXOzSnByEojNilU1Eq6D4eZJ+YlU26mFafuhoTrgAys0WprXUqWFN7267jPkfN6 SEhMbt9wab9tHrWTkrt9t024fHduKn+hY6Nc89xl2RT/q1AjE3g7tD3Z3T1JbzLD6MaY gR7DeOeB1Bteo+lRz6SkiqcHMbXg9gzylRPbqs1OjwjXxKon7wRX47is1jDN138d9lla e0kVe0+Kja7a/tgGB7mWJml5eF50gFCkkGGtHx9KqXiVRy0YpkPoW2YE53f7qRGm5v0C fmbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=onbr90sVjfwzLUpMin+kqCkGwYYzgs4DWvECoUuKee0=; b=AnIVxjm/0HeHCuwXxHnPkSYPwJBfZ5Bsm2HOmfprvAFIJWQPoUdpmc1R2mhdIuCtEK WlDBXzy4OCjvu70qAc2Atq3CZOe7xuV7nOL++xuhM5qJWqZH2Mg+YgPu5iw3EX5fGly7 Vyq8vXUr6cWUNPbld2Di3RXZFZsc6+VULw0+Hrb2ws42WFf7VafuYgZjmkeKfdE3HMpE w/VqDphb52+YJsfswBG2Ywn18OOFFQB4OEdupzVHWtsEDwipMkwTIQlYzNh8D9P3hzEW QJE2UZVymkBCt6GbBCxjcHw7cyYv7hfZzgFnPJUWHEBofsVDtLIksZQmwgkqKEqzouLE owXg== ARC-Authentication-Results: i=1; mx.google.com; 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 l84si3565020oig.5.2020.02.06.23.58.08; Thu, 06 Feb 2020 23:58:23 -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; 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 S1726642AbgBGH4n (ORCPT + 99 others); Fri, 7 Feb 2020 02:56:43 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:9708 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726130AbgBGH4n (ORCPT ); Fri, 7 Feb 2020 02:56:43 -0500 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id DB1AA3694BE73AD34D31; Fri, 7 Feb 2020 15:56:38 +0800 (CST) Received: from HGHY2S004841851.china.huawei.com (10.184.217.114) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.439.0; Fri, 7 Feb 2020 15:56:32 +0800 From: Shen Kai To: , CC: , , Subject: [PATCH] add lock proctect to __handle_sysrq in write_sysrq_trigger Date: Fri, 7 Feb 2020 07:56:06 +0000 Message-ID: <1581062166-27284-1-git-send-email-shenkai8@huawei.com> X-Mailer: git-send-email 2.6.4.windows.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.184.217.114] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Feilong Lin Add lock protect to __handle_sysrq to avoid race condition. __handle_sysrq will change console_loglevel without lock protect which can lead to console_loglevel to be set as an unexpected value. Problem may occur when "echo t > /proc/sysrq-trigger" is called on multiple cpus concurrently. In this case in __handle_sysrq, console_loglevel is set to 7 to print some head info to the console then restore it. But without lock protect in parallel execution situation, restoring may go wrong. The new loglevel may be taken as the previous loglevel incorrectly. Console_loglevel can be 7 at last, which causes the terminal to output info in most log levels. This bug was found on linux 4.19 Signed-off-by: Feilong Lin Reported-by: Kai Shen --- drivers/tty/sysrq.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c index f724962..cbb48a9 100644 --- a/drivers/tty/sysrq.c +++ b/drivers/tty/sysrq.c @@ -1087,6 +1087,8 @@ EXPORT_SYMBOL(unregister_sysrq_key); /* * writing 'C' to /proc/sysrq-trigger is like sysrq-C */ +static DEFINE_MUTEX(sysrq_mutex); + static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { @@ -1095,7 +1097,9 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, if (get_user(c, buf)) return -EFAULT; + mutex_lock(&sysrq_mutex); __handle_sysrq(c, false); + mutex_unlock(&sysrq_mutex); } return count; -- 2.6.4.windows.1