Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp822908pxb; Tue, 3 Nov 2020 13:33:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJxW6LNc+KcXYT38g9AQY5/LzVEWTAQAs4NX/3g9qxDlsjMbv2jjBilSjsDDSNcnLgTmpK/1 X-Received: by 2002:a05:6402:54c:: with SMTP id i12mr267262edx.9.1604439183379; Tue, 03 Nov 2020 13:33:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604439183; cv=none; d=google.com; s=arc-20160816; b=l4FRj/tqnhxQPJ+ZKSW2QGpjbrzwkaC13KlGMhCwfmS43R3Y+XhoaUhkXNK+XiIXYv dOTT/74ThTMqd9q2L8uthDP20XJD58kB/56oDJldWC58owmOYcEQaxuFx1HR+Y8b/C8c pkMiTtwqYCQXxa28CEnVFeptfbjrOJgiMj228voIgSJmDqjBB9R/Zuf3xaI9L54c7Eak Zraf4oW/rGRWPo7iJIj9E5GV54yGmdqQ7ddpjmDlpyPpg3aGsHg+43K4UItun3nIh7WZ 3H8JfN7O8N24pg3KFRe/zP7c+f8xy+ipiowI0/b8pG5wuwlRvzMfhE1kKWOwEwEd6yqX V0mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WVffodqpq/DZg68dLxo2gGk8tWKZtAJPC6a2j8OrVog=; b=lZR+EGAMNDsLpsi9aisv9eWOOWnyP9YqiFEZOKBm4rRBEV4bZS1gYwRfT/JCC0268+ rECyZbifOo8Iu7GWYHU9ro+ESdb8TaiycFU3CvuDkiGILI/CGUtMXJUHy9b7ht9AcqiF Ca7DFhscs2cMXKKgEZtuG/rIdk35z22kqniTLSKgwz3cJVkeaGDulnJvuzmQexdaECbP eCiEYfaZP2ZyOVDB4zNF/8CM0JYdXjJkN7Iu7UIwbvrE8sJXGc6WRDpoxU2yhvK/gHoc Q6tnkt1N07lCfk52s1PUmYZkmu3dHaYVlJTkBhFtJ0D6nWLjVh/44ZWWG5WQ5VjvK/ru wmig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=J9E4hQ39; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p10si14528ejy.70.2020.11.03.13.32.40; Tue, 03 Nov 2020 13:33:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=J9E4hQ39; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732985AbgKCVaG (ORCPT + 99 others); Tue, 3 Nov 2020 16:30:06 -0500 Received: from mail.kernel.org ([198.145.29.99]:32812 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732964AbgKCU6Y (ORCPT ); Tue, 3 Nov 2020 15:58:24 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 C16FE223C6; Tue, 3 Nov 2020 20:58:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604437102; bh=/Ks8KETB+CKrRYrGo2T+5rqEmySgJfur65rlwYhBuKo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J9E4hQ39tqeGl6+ASEaax1zQ6aSPpmHYJC4zDUegocLhOIAJJcxplBK20ne4axIRa +FMYoZfhr4UDf0cZgwHLPn9XCBugaKxF2wlqjzeN1WtCQjDqTUVHCTs8tFHVR/DIgv 6B97OiGW7+Y3RiHLtZTxgdg8qxoiVX94fWNM8CZA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Minh Yuan , Jiri Slaby Subject: [PATCH 5.4 146/214] vt: keyboard, extend func_buf_lock to readers Date: Tue, 3 Nov 2020 21:36:34 +0100 Message-Id: <20201103203304.496720491@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203249.448706377@linuxfoundation.org> References: <20201103203249.448706377@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiri Slaby commit 82e61c3909db51d91b9d3e2071557b6435018b80 upstream. Both read-side users of func_table/func_buf need locking. Without that, one can easily confuse the code by repeatedly setting altering strings like: while (1) for (a = 0; a < 2; a++) { struct kbsentry kbs = {}; strcpy((char *)kbs.kb_string, a ? ".\n" : "88888\n"); ioctl(fd, KDSKBSENT, &kbs); } When that program runs, one can get unexpected output by holding F1 (note the unxpected period on the last line): . 88888 .8888 So protect all accesses to 'func_table' (and func_buf) by preexisting 'func_buf_lock'. It is easy in 'k_fn' handler as 'puts_queue' is expected not to sleep. On the other hand, KDGKBSENT needs a local (atomic) copy of the string because copy_to_user can sleep. Use already allocated, but unused 'kbs->kb_string' for that purpose. Note that the program above needs at least CAP_SYS_TTY_CONFIG. This depends on the previous patch and on the func_buf_lock lock added in commit 46ca3f735f34 (tty/vt: fix write/write race in ioctl(KDSKBSENT) handler) in 5.2. Likely fixes CVE-2020-25656. Cc: Reported-by: Minh Yuan Signed-off-by: Jiri Slaby Link: https://lore.kernel.org/r/20201019085517.10176-2-jslaby@suse.cz Signed-off-by: Greg Kroah-Hartman --- drivers/tty/vt/keyboard.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) --- a/drivers/tty/vt/keyboard.c +++ b/drivers/tty/vt/keyboard.c @@ -742,8 +742,13 @@ static void k_fn(struct vc_data *vc, uns return; if ((unsigned)value < ARRAY_SIZE(func_table)) { + unsigned long flags; + + spin_lock_irqsave(&func_buf_lock, flags); if (func_table[value]) puts_queue(vc, func_table[value]); + spin_unlock_irqrestore(&func_buf_lock, flags); + } else pr_err("k_fn called with value=%d\n", value); } @@ -1990,7 +1995,7 @@ out: #undef s #undef v -/* FIXME: This one needs untangling and locking */ +/* FIXME: This one needs untangling */ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) { struct kbsentry *kbs; @@ -2022,10 +2027,14 @@ int vt_do_kdgkb_ioctl(int cmd, struct kb switch (cmd) { case KDGKBSENT: { /* size should have been a struct member */ - unsigned char *from = func_table[i] ? : ""; + ssize_t len = sizeof(user_kdgkb->kb_string); + + spin_lock_irqsave(&func_buf_lock, flags); + len = strlcpy(kbs->kb_string, func_table[i] ? : "", len); + spin_unlock_irqrestore(&func_buf_lock, flags); - ret = copy_to_user(user_kdgkb->kb_string, from, - strlen(from) + 1) ? -EFAULT : 0; + ret = copy_to_user(user_kdgkb->kb_string, kbs->kb_string, + len + 1) ? -EFAULT : 0; goto reterr; }