Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp25580ybp; Thu, 3 Oct 2019 09:40:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqwY+NWjuc91/fBS8B9GRFkn1CWMFZucxA8kHinZaiQH274mfXARzuU8XcRTwURvjq0uanN+ X-Received: by 2002:a17:906:699:: with SMTP id u25mr8274035ejb.250.1570120842056; Thu, 03 Oct 2019 09:40:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570120842; cv=none; d=google.com; s=arc-20160816; b=zZQR3O0/yTtTfXct+8HvZEyjsyeivgee6MIEC65/Gp0/N0YHjdw2ivPKMbekBf6zGm utF1LrL5FeE1yu0wFHEM5uNyDPqXDaX91OIOzY1uXifFTfeVTPeWr+IPWpCIc/qhmo0Z GoiheYZEYR5J5bENK7O0Ci9Uj7VzDbeQfQUZ3Fg/Ay9haucHouKh/wiqbVJAN6hwZIO0 gheMZtBPCfzj8tHAMhdbeKWnQ2eeGCaQjcWPqUqngFlMlCVI3Ffn36lv/0Bjs0s/Re3h dn+gOHgy2vrDR0rC9QPfIouPAdmlacPJvVEsYxGBqEVbtNVLZpXwwWceaksWcO3lXJ3C dp8Q== 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=ddBPZeympRaD6pAv14BN9uqveffthV9sufQ/F1KT5RU=; b=VBbm+WoHaWHadSSgU9rlY5J9Zoea6X128bi5843UtTflmmWKI9ooxvuQaCuL7Ufge1 lTZKgIL7ugmCGI92WVsLmNfSRqZ4VAAcrrD/wRRLl+kHtXHO057xxIpZxytnVgcpU7D8 CA6JWiuVMMrGlo/z3YOReGhdFzKESZXWYoiBnAOlVmtnSHATrsNoekVKwS5X81NxWMl2 GqvmhtKmZz7/F3RJHgiQYaDqq2KOclWVD02dt9nNadITQjOkL6j92V63Lf7n+L1aj42U ApU5R10WhDTpjRJeqXtF/JsI9BTtqN3CDZxgDcmgNphGjUd6CWfj1WTCeSUkYzGr9tnS vFEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CpZxkbpS; 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 z55si1903173edz.254.2019.10.03.09.40.16; Thu, 03 Oct 2019 09:40:42 -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=CpZxkbpS; 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 S2404554AbfJCQhV (ORCPT + 99 others); Thu, 3 Oct 2019 12:37:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:46618 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404537AbfJCQhP (ORCPT ); Thu, 3 Oct 2019 12:37:15 -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 8E1A6215EA; Thu, 3 Oct 2019 16:37:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570120634; bh=uAVntkjPLgHA5UmSzzCWhj6ohpZsu17PsT4M5wIMo7M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CpZxkbpSo+0LF3G1cbP7OPIujnP3KNJyURjyoFVC40IyCN2n2efLXmqL6dftT9Iwl b/GCiXK/qavPAPxGN6/6bywX6DMy5Q9ALE/b9mHsw/UJClplWFn385972iP960K5Lj KGoeTc0K7B42jzE3v0OGMJHsmy5thAA4AMFbu1A4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tetsuo Handa , syzbot Subject: [PATCH 5.2 296/313] /dev/mem: Bail out upon SIGKILL. Date: Thu, 3 Oct 2019 17:54:34 +0200 Message-Id: <20191003154602.334688993@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191003154533.590915454@linuxfoundation.org> References: <20191003154533.590915454@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: Tetsuo Handa commit 8619e5bdeee8b2c685d686281f2d2a6017c4bc15 upstream. syzbot found that a thread can stall for minutes inside read_mem() or write_mem() after that thread was killed by SIGKILL [1]. Reading from iomem areas of /dev/mem can be slow, depending on the hardware. While reading 2GB at one read() is legal, delaying termination of killed thread for minutes is bad. Thus, allow reading/writing /dev/mem and /dev/kmem to be preemptible and killable. [ 1335.912419][T20577] read_mem: sz=4096 count=2134565632 [ 1335.943194][T20577] read_mem: sz=4096 count=2134561536 [ 1335.978280][T20577] read_mem: sz=4096 count=2134557440 [ 1336.011147][T20577] read_mem: sz=4096 count=2134553344 [ 1336.041897][T20577] read_mem: sz=4096 count=2134549248 Theoretically, reading/writing /dev/mem and /dev/kmem can become "interruptible". But this patch chose "killable". Future patch will make them "interruptible" so that we can revert to "killable" if some program regressed. [1] https://syzkaller.appspot.com/bug?id=a0e3436829698d5824231251fad9d8e998f94f5e Signed-off-by: Tetsuo Handa Cc: stable Reported-by: syzbot Link: https://lore.kernel.org/r/1566825205-10703-1-git-send-email-penguin-kernel@I-love.SAKURA.ne.jp Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/char/mem.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) --- a/drivers/char/mem.c +++ b/drivers/char/mem.c @@ -97,6 +97,13 @@ void __weak unxlate_dev_mem_ptr(phys_add } #endif +static inline bool should_stop_iteration(void) +{ + if (need_resched()) + cond_resched(); + return fatal_signal_pending(current); +} + /* * This funcion reads the *physical* memory. The f_pos points directly to the * memory location. @@ -175,6 +182,8 @@ static ssize_t read_mem(struct file *fil p += sz; count -= sz; read += sz; + if (should_stop_iteration()) + break; } kfree(bounce); @@ -251,6 +260,8 @@ static ssize_t write_mem(struct file *fi p += sz; count -= sz; written += sz; + if (should_stop_iteration()) + break; } *ppos += written; @@ -468,6 +479,10 @@ static ssize_t read_kmem(struct file *fi read += sz; low_count -= sz; count -= sz; + if (should_stop_iteration()) { + count = 0; + break; + } } } @@ -492,6 +507,8 @@ static ssize_t read_kmem(struct file *fi buf += sz; read += sz; p += sz; + if (should_stop_iteration()) + break; } free_page((unsigned long)kbuf); } @@ -544,6 +561,8 @@ static ssize_t do_write_kmem(unsigned lo p += sz; count -= sz; written += sz; + if (should_stop_iteration()) + break; } *ppos += written; @@ -595,6 +614,8 @@ static ssize_t write_kmem(struct file *f buf += sz; virtr += sz; p += sz; + if (should_stop_iteration()) + break; } free_page((unsigned long)kbuf); }