From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5756318EDF for ; Fri, 26 Jun 2026 17:41:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782495684; cv=none; b=FSyFyM26LN2f8jlI7M84GlQRaLuaoS1GAFNOExqcewFceysnvoPTtCsvuU67OHLR5r81W4nSBh8kr/s/1WL0e+FjOlMYYnssic8+PwHUe/B7b9ggkNPdLtJsZV1kDzleUjhPQGtHF7EAVM0zYEkyZPuM9zcfAdSvvQv972kJfHw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782495684; c=relaxed/simple; bh=spoWBCg4J7mJjlelimvdL+ViLF52tlXAhfBCgqFT42Y=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=hp+bjZF2U+5Wy1A04Ew0nQALQPoynDqj9YRflW6bgqN4a6yLi4wJcPAe6XevrlI5qmgvzkqtCLPQbRvsbxIPgUl+4Xz8DxsrdH/2NEzCE8S7FK8pCX9A5k+ZNhgevIYSLGLEslYeoxvARPn0pb5/DFEaCf7OK9ttdvXkIPQbtFg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=VK1ft2UG; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VK1ft2UG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1782495681; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=3v8iJ8R+RvjqaZ2CbiATyZAH06Q+JiTc+B91q5HvxTw=; b=VK1ft2UGcXhOV0x4nmRhwkPpbL+GGGR1x+ZxgzHDaRomXuiycW2X+sdPOvg/DRPLX4ZJ2l pdwUGTMhIcGPgf0kTfJ1oiOpyDBOqnfLJ3SftbkRKVRqB6Ha+akAx5JsUhfcdB0qPelnKi EnvS+xY6Z4umAuSuBysVZhwK1bW3faw= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-650-MOySjg1kO0OIZa7fWs0leQ-1; Fri, 26 Jun 2026 13:41:20 -0400 X-MC-Unique: MOySjg1kO0OIZa7fWs0leQ-1 X-Mimecast-MFC-AGG-ID: MOySjg1kO0OIZa7fWs0leQ_1782495679 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7B25B1955D53; Fri, 26 Jun 2026 17:41:18 +0000 (UTC) Received: from work.redhat.com (unknown [10.44.48.194]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D79BC1800599; Fri, 26 Jun 2026 17:41:15 +0000 (UTC) From: Tim Wiederhake To: "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, linux-kernel@vger.kernel.org Cc: Tim Wiederhake Subject: [PATCH] x86/msr: Restrict /dev/cpu/*/msr read and write to a single MSR per call Date: Fri, 26 Jun 2026 19:40:36 +0200 Message-ID: <20260626174037.1128563-1-twiederh@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Reading or writing /dev/cpu/*/msr with a buffer larger than 8 bytes reads or writes not successive MSRs, but the same one over and over. While documented, this behavior is unintuitive and not helpful. Remove the loops and process a single 8-byte MSR value per call. Signed-off-by: Tim Wiederhake --- This is a resend of a patch originally submitted in May 2023 [1], updated to also cover msr_write and rebased to v7.1. [1] https://lkml.org/lkml/2023/5/23/1230 arch/x86/kernel/msr.c | 48 +++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c index 4469c784eaa0..528a8e9b2319 100644 --- a/arch/x86/kernel/msr.c +++ b/arch/x86/kernel/msr.c @@ -10,8 +10,7 @@ * x86 MSR access device * * This device is accessed by lseek() to the appropriate register number - * and then read/write in chunks of 8 bytes. A larger size means multiple - * reads or writes of the same register. + * and then read/write in chunks of 8 bytes. * * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on * an SMP box will direct the access to CPU %d. @@ -57,24 +56,17 @@ static ssize_t msr_read(struct file *file, char __user *buf, u32 reg = *ppos; int cpu = iminor(file_inode(file)); int err = 0; - ssize_t bytes = 0; - if (count % 8) + if (count < 8) return -EINVAL; /* Invalid chunk size */ - for (; count; count -= 8) { - err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]); - if (err) - break; - if (copy_to_user(tmp, &data, 8)) { - err = -EFAULT; - break; - } - tmp += 2; - bytes += 8; - } + err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]); + if (err) + return err; + if (copy_to_user(tmp, &data, 8)) + return -EFAULT; - return bytes ? bytes : err; + return 8; } static int filter_write(u32 reg) @@ -113,7 +105,6 @@ static ssize_t msr_write(struct file *file, const char __user *buf, u32 reg = *ppos; int cpu = iminor(file_inode(file)); int err = 0; - ssize_t bytes = 0; err = security_locked_down(LOCKDOWN_MSR); if (err) @@ -123,26 +114,19 @@ static ssize_t msr_write(struct file *file, const char __user *buf, if (err) return err; - if (count % 8) + if (count < 8) return -EINVAL; /* Invalid chunk size */ - for (; count; count -= 8) { - if (copy_from_user(&data, tmp, 8)) { - err = -EFAULT; - break; - } - - add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); + if (copy_from_user(&data, tmp, 8)) + return -EFAULT; - err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]); - if (err) - break; + add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); - tmp += 2; - bytes += 8; - } + err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]); + if (err) + return err; - return bytes ? bytes : err; + return 8; } static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg) -- 2.52.0