Received: by 2002:a05:6622:f08:0:0:0:0 with SMTP id l8csp4480854ivc; Tue, 3 Nov 2020 13:09:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyjf3br9sUuU7GToNwurJ6SrtD9KupepJn2u0s4st3zvAdDG7NZLeouuPqpqS2FEZUGyust X-Received: by 2002:aa7:dd42:: with SMTP id o2mr23389335edw.53.1604437762407; Tue, 03 Nov 2020 13:09:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604437762; cv=none; d=google.com; s=arc-20160816; b=D5h+pmizZl04ef1sZkbkMGogud+neX0e5EsGsUrJXnla0ltQ2s56kqDl6m9osFibNZ RcUtuN+yAbB05ga/Qw1oyo6DsNiIv5gsBFfD/VuG89uMFYYV/JmNXrhXdbswAvg6pkae BWe+tcXGXPFcWN4R1f0CDBpP4QNgK7il5sx3BGo3h5gaLpu3WVFwNWM1pRM9C9w7URhp sB3HxI3TxaQEP1oET/V/a5dzICNXGw7z7MBlZK6AVYLmEdEMc2+1bKzfQeeqKVaGQtJI syy2bKV22ctv7rXTcplQq6n4mHIhB/fXdJZWQ0scji+sgzk/k1liu1LiGxDI9GE+kk4b em3w== 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=Ioux3flU8tsTG0hVqNkhDltE07rBWa/s50x2pdIC2pzP/W5QcSkfZUeEfo1nwe8VEu jWFm7VNV/q6dqx24QSiXkvmk4oA9v/cYOAfDwMNOGET3jFTX0pmSoalUbqL87I89fOMR qZekF2QCRIfkNAvopR1hz9/s8v9nuisnWQHJSAR3AOKNco1TLFPO+qm+N8aypf+v64d1 fL2r5yxrZnaEPzY5OruglptN1OyWIb/mAhbNBavs8EgDuTfgA8zOQA12Pz4QzC5GI6Me tXZ0IJrf1/HSQcu3t97OcRu0IQy69jJAboZ1zuItX/lo01jMcvGgeZPViOIy+Il/i5q8 7P7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=e+CtMD5P; 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 d18si13741223edx.345.2020.11.03.13.08.58; Tue, 03 Nov 2020 13:09:22 -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=e+CtMD5P; 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 S2388196AbgKCVGi (ORCPT + 99 others); Tue, 3 Nov 2020 16:06:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:45606 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733232AbgKCVGh (ORCPT ); Tue, 3 Nov 2020 16:06:37 -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 06980206CB; Tue, 3 Nov 2020 21:06:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604437596; bh=/Ks8KETB+CKrRYrGo2T+5rqEmySgJfur65rlwYhBuKo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e+CtMD5PPzONh4bXbEtZGYNpKl9vEtnZot8v/72C9rJC32zW6/n7ElnA0t3fmAMP+ T5wLeJVjzYAU2eoSKmjHGvLpS+ZZEiwjhbTJmDU6Kx/vDZLC1F0EAR/K9Xh6VIBVqT CBbPmEW+fd8i3GXSDnEOp3Wav8s7lZ2zjIqC2k+U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Minh Yuan , Jiri Slaby Subject: [PATCH 4.19 145/191] vt: keyboard, extend func_buf_lock to readers Date: Tue, 3 Nov 2020 21:37:17 +0100 Message-Id: <20201103203246.361801167@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203232.656475008@linuxfoundation.org> References: <20201103203232.656475008@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; }