mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mike Crowe <mac@mcrowe.com>
To: linux-kernel@vger.kernel.org
Cc: Mike Crowe <mac@mcrowe.com>, Arnd Bergmann <arnd@arndb.de>,
	Alexander Sverdlin <alexander.sverdlin@nokia.com>,
	stable@vger.kernel.org
Subject: [PATCH] kmsg: Return -ESPIPE rather than EBADF from llseek(SEEK_CUR)
Date: Wed, 22 Dec 2021 11:30:54 +0000	[thread overview]
Message-ID: <20211222113054.673553-1-mac@mcrowe.com> (raw)

glibc's dprintf implementation tries to determine the current file position
by calling lseek(fd, 0, SEEK_CUR). Unfortunately it treats receiving EINVAL
as an error. See https://sourceware.org/bugzilla/show_bug.cgi?id=17830

From what I can tell prior to Kay Sievers printk record commit
e11fea92e13fb91c50bacca799a6131c81929986, calling lseek(fd, 0, SEEK_CUR)
with such a file descriptor would not return an error.

Prior to Kay's change, Arnd Bergmann's commit
6038f373a3dc1f1c26496e60b6c40b164716f07e seemed to go to some lengths to
preserve the successful return code rather than returning (the perhaps more
logical) -ESPIPE.

glibc is happy with either a successful return or -ESPIPE. It seems that
the consensus is to return -ESPIPE in this situation.

Alexander Sverdlin supplied this test case:
--8<--
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <fcntl.h>

 int main(int argc, char **argv)
 {
         return dprintf(open("/dev/kmsg", O_WRONLY), "\n") < 0;
 }
-->8--

A variant of this fix was originally proposed in 2015[1]. The problem
was woken up again in 2019[2] and this fix was posted[3] shortly
afterwards, but it did not land. This version has been rebased to fix
trivial conflicts.

[1] https://lkml.org/lkml/2015/1/15/575
[2] https://lkml.org/lkml/2019/3/21/172
[3] https://lkml.org/lkml/2019/3/24/279

Signed-off-by: Mike Crowe <mac@mcrowe.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Tested-by: Alexander Sverdlin <alexander.sverdlin@nokia.com>
Cc: stable@vger.kernel.org
Fixes: e11fea92e1 ("kmsg: export printk records to the /dev/kmsg interface")
---
 kernel/printk/printk.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 57b132b658e1..6013ea991378 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -803,6 +803,11 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
 		/* after the last record */
 		atomic64_set(&user->seq, prb_next_seq(prb));
 		break;
+	case SEEK_CUR:
+		/* For compatibility with userspace expecting SEEK_CUR
+		 * to not yield EINVAL. */
+		ret = -ESPIPE;
+		break;
 	default:
 		ret = -EINVAL;
 	}
-- 
2.30.2


             reply	other threads:[~2021-12-22 11:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-22 11:30 Mike Crowe [this message]
  -- strict thread matches above, loose matches on Subject: below --
2019-03-24 10:27 Mike Crowe

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=20211222113054.673553-1-mac@mcrowe.com \
    --to=mac@mcrowe.com \
    --cc=alexander.sverdlin@nokia.com \
    --cc=arnd@arndb.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.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

all inboxes | Powered by JetHome®