Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4656194rdb; Tue, 12 Dec 2023 06:03:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IGQod8eWzRMccROZjGW9RuTk79uEECi8nS+4mXnPxLX4ySF6Wf5OD0GGra09mar57QCSDs1 X-Received: by 2002:a05:6870:332b:b0:1fa:fd1e:75ce with SMTP id x43-20020a056870332b00b001fafd1e75cemr6296627oae.6.1702389807480; Tue, 12 Dec 2023 06:03:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702389807; cv=none; d=google.com; s=arc-20160816; b=CeyfcIn7gyKmb13H7+G2hnYs0S/H/O8oEMF6AoWF4LFkCnRhKFpbhL4elUDriM2Z4Z 2k7q2JVWw0zfWeWzSmkTvkQVyNyJwTbXGnh47Yl+kAxd7bMqJlqCl+clBWspYZ7SuGsk RbKhIOKlV/iupBDmAgFCIecW9ctQl/vpKX9eAqvmVnSK03sffCYCCv/T40rkseoR7QUH 5rlDEszgyRmRoQgYpHbuCJnpnFHDcFO7MxmMfAXFSWaxYRJao3KPqhVKqgZ4pMkQ9UCA athCc40nZaQapX6uHSiIHCQ5bCrhBlUVuwierMUkOa7JO2cBCxyhCOBpjfEI5KXRM9+N jrFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=uZijd03LXNPYjeHZjeC7Jq869Ey7f+8xrdH6Ffypg54=; fh=k5dmERTjJsDZlpijj+efv2kprEA+Pe/ZVqrTaR6mHt8=; b=q7uOkd4rnyhqAYbV5keFSCLz9Ak9plg4osuwmh3rUIGZKV7gLQV1WOgg1cV3YLjzh+ GU47iBpbZi+KBKVLo+68UD+czSrJneNyPkxpt13DG1I2ZMFVN4Ea0CZMzQn5b8T+ftfk ltU4WgR1dQgqKHT33uXhZeXZnfQ30hqp3uXtTMrBoI9WZ3hWPeTnHxUrL9+BAp438Dh1 DNLTkbg5PZ+zZXGqGKcCm4fpSaQbn7CVOyWWUFMmx8YEetTWnbQMRKJWxBArUvztlMNo 6FCSemsnpiVxB+LnkvMasNDY9vIXzid6w49pd+maxAzM+u3LrV+9bzeSkLicMSU6vbtN fypQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=k8yk9r4u; spf=pass (google.com: domain of linux-crypto+bounces-755-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-755-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ti4-20020a056871890400b001fb2f8ef45fsi3271346oab.270.2023.12.12.06.03.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 06:03:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto+bounces-755-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=k8yk9r4u; spf=pass (google.com: domain of linux-crypto+bounces-755-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-755-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BBD00281B1F for ; Tue, 12 Dec 2023 14:03:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C563873167; Tue, 12 Dec 2023 14:03:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="k8yk9r4u" X-Original-To: linux-crypto@vger.kernel.org Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47C6CB3; Tue, 12 Dec 2023 06:03:18 -0800 (PST) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a1f653e3c3dso585140166b.2; Tue, 12 Dec 2023 06:03:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702389797; x=1702994597; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=uZijd03LXNPYjeHZjeC7Jq869Ey7f+8xrdH6Ffypg54=; b=k8yk9r4uUufQsTcW5zt2CoXBMz3EjZuRFeoupN0Vx2JVhwiGPZE1SxbyDaKbvQryrX nSOJEt6HJpNzQt4B7uvQA7eWIuhu8sktmV5CK0NJwSe+7tD6q39P4fNOXUcuJhPfBYqs WryZEZ2oawM9LMh+INpuuIfjCkhmKobA9gZr0kzQfuwBLgRpd5xvFUL5q9vxZL9oi1jb alHRuCuzMXi4YtV4FnpGwF61jAvO/6tOeYV8NxYwfmGHDWl6ObhFybLC9wu+qdTytSox ODrE+yN9XnAxYLN2mZXW5riRDYRDXw7RwxakftrOybhDuOj0GYqJ4G9Bu0oEtwtjrSE+ Ji+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702389797; x=1702994597; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uZijd03LXNPYjeHZjeC7Jq869Ey7f+8xrdH6Ffypg54=; b=G7+ttSaFhMfPmpQRCn2H8UtcriCTtQ4DrR0JpTUuJad2bmsRNiPW2EL15YfacFlLB5 7CJi1tJ802J/kXgzhLMzRw9Sm/N7YH2wPInUsQaSqCGWZPbFTgV36Cux9Wm5uogVcBl6 dNoTv3/Ky5+66UkIH1bRKoTTd4ovQyEHoQZC6TB56Ewyxl+w79763+ph8nS+mdsDJ2w7 q82yDzO60zzp+NrMH4g3uAVedwTkDoURiviyJ5N1Bk4irUqr9zXQN2M8+3eDbWirHwos Ak4xYqX8GqcT+cJ59enMPa6BrL2Qzl23jYOZLgkVe+Gy/yRUtCAbtorM1X7sue5mPUaB Jlng== X-Gm-Message-State: AOJu0YzCn7OzzeB3KwRkt4xVnCQl9rLd/C6j9rYlH3KNEJuXUGWfl3AP TqS9nU3XCAJQvW1GYEwEg7+yCLWoRFsI/zPmhx0= X-Received: by 2002:a17:906:1f4d:b0:a19:a1ba:bae7 with SMTP id d13-20020a1709061f4d00b00a19a1babae7mr1523005ejk.141.1702389796323; Tue, 12 Dec 2023 06:03:16 -0800 (PST) Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: In-Reply-To: From: PrasannaKumar Muralidharan Date: Tue, 12 Dec 2023 19:33:04 +0530 Message-ID: Subject: Re: [PATCH] hwrng: core - Fix page fault dead lock on mmap-ed hwrng To: Herbert Xu Cc: Edward Adam Davis , davem@davemloft.net, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, olivia@selenic.com, syzbot+c52ab18308964d248092@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com, Ian Molton , Rusty Russell Content-Type: text/plain; charset="UTF-8" On Sat, 2 Dec 2023 at 08:05, Herbert Xu wrote: > > There is a dead-lock in the hwrng device read path. This triggers > when the user reads from /dev/hwrng into memory also mmap-ed from > /dev/hwrng. The resulting page fault triggers a recursive read > which then dead-locks. > > Fix this by using a stack buffer when calling copy_to_user. > > Reported-by: Edward Adam Davis > Reported-by: syzbot+c52ab18308964d248092@syzkaller.appspotmail.com > Fixes: 9996508b3353 ("hwrng: core - Replace u32 in driver API with byte array") > Cc: > Signed-off-by: Herbert Xu > > diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c > index 420f155d251f..a3bbdd6e60fc 100644 > --- a/drivers/char/hw_random/core.c > +++ b/drivers/char/hw_random/core.c > @@ -23,10 +23,13 @@ > #include > #include > #include > +#include > #include > > #define RNG_MODULE_NAME "hw_random" > > +#define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES) > + > static struct hwrng *current_rng; > /* the current rng has been explicitly chosen by user via sysfs */ > static int cur_rng_set_by_user; > @@ -58,7 +61,7 @@ static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size, > > static size_t rng_buffer_size(void) > { > - return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES; > + return RNG_BUFFER_SIZE; > } > > static void add_early_randomness(struct hwrng *rng) > @@ -209,6 +212,7 @@ static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size, > static ssize_t rng_dev_read(struct file *filp, char __user *buf, > size_t size, loff_t *offp) > { > + u8 buffer[RNG_BUFFER_SIZE]; > ssize_t ret = 0; > int err = 0; > int bytes_read, len; > @@ -236,34 +240,37 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf, > if (bytes_read < 0) { > err = bytes_read; > goto out_unlock_reading; > - } > - data_avail = bytes_read; > - } > - > - if (!data_avail) { > - if (filp->f_flags & O_NONBLOCK) { > + } else if (bytes_read == 0 && > + (filp->f_flags & O_NONBLOCK)) { > err = -EAGAIN; > goto out_unlock_reading; > } > - } else { > - len = data_avail; > + > + data_avail = bytes_read; > + } > + > + len = data_avail; > + if (len) { > if (len > size) > len = size; > > data_avail -= len; > > - if (copy_to_user(buf + ret, rng_buffer + data_avail, > - len)) { > + memcpy(buffer, rng_buffer + data_avail, len); > + } > + mutex_unlock(&reading_mutex); > + put_rng(rng); > + > + if (len) { > + if (copy_to_user(buf + ret, buffer, len)) { > err = -EFAULT; > - goto out_unlock_reading; > + goto out; > } > > size -= len; > ret += len; > } > > - mutex_unlock(&reading_mutex); > - put_rng(rng); > > if (need_resched()) > schedule_timeout_interruptible(1); > @@ -274,6 +281,7 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf, > } > } > out: > + memzero_explicit(buffer, sizeof(buffer)); > return ret ? : err; > > out_unlock_reading: > -- > Email: Herbert Xu > Home Page: http://gondor.apana.org.au/~herbert/ > PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt > Reviewed-by: PrasannaKumar Muralidharan Regards, PrasannaKumar