Linux Power Management development
 help / color / mirror / Atom feed
From: ankun <ankun@uniontech.com>
To: rafael@kernel.org
Cc: pavel@kernel.org, lenb@kernel.org, linux-pm@vger.kernel.org,
	ankun <ankun@uniontech.com>
Subject: [PATCH] PM: Avoid TOCTOU race in pm_wakeup_irq_show()
Date: Thu, 11 Jun 2026 11:32:13 +0800	[thread overview]
Message-ID: <20260611033213.11252-1-ankun@uniontech.com> (raw)

pm_wakeup_irq_show() reads the global pm_wakeup_irq variable twice:
first to check if it is zero (returning -ENODATA if so), and then again
to format the value for sysfs output. Between these two calls, the
wakeup IRQ could be cleared (e.g. by writing 0 to the same sysfs file),
leading to a time-of-check-to-time-of-use race. The result may be a
printed value of 0 despite the check having passed, or other
inconsistencies.

Cache the value in a local variable after a single read, and use that
for both the check and the output. This closes the race window and
makes the operation atomic from the caller's perspective.

Signed-off-by: ankun <ankun@uniontech.com>
---
 kernel/power/main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/kernel/power/main.c b/kernel/power/main.c
index 5429e9f19b65..08a25117e38c 100644
--- a/kernel/power/main.c
+++ b/kernel/power/main.c
@@ -683,10 +683,12 @@ static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
 					struct kobj_attribute *attr,
 					char *buf)
 {
-	if (!pm_wakeup_irq())
+	unsigned int irq = pm_wakeup_irq();
+
+	if (!irq)
 		return -ENODATA;

-	return sysfs_emit(buf, "%u\n", pm_wakeup_irq());
+	return sysfs_emit(buf, "%u\n", irq);
 }

 power_attr_ro(pm_wakeup_irq);
--
2.20.1


                 reply	other threads:[~2026-06-11  3:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260611033213.11252-1-ankun@uniontech.com \
    --to=ankun@uniontech.com \
    --cc=lenb@kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pavel@kernel.org \
    --cc=rafael@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox