From: akpm@linux-foundation.org Subject: [patch 11/11] VFS: lseek(fd, 0, SEEK_CUR) race condition Date: Mon, 01 Dec 2008 14:35:01 -0800 Message-ID: <200812012235.mB1MZ2Hm021167@imap1.linux-foundation.org> Cc: linux-fsdevel@vger.kernel.org, akpm@linux-foundation.org, alain@knaff.lu, hch@lst.de To: viro@zeniv.linux.org.uk Return-path: Received: from smtp1.linux-foundation.org ([140.211.169.13]:56174 "EHLO smtp1.linux-foundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752551AbYLAWmx (ORCPT ); Mon, 1 Dec 2008 17:42:53 -0500 Sender: linux-fsdevel-owner@vger.kernel.org List-ID: From: Alain Knaff This patch fixes a race condition in lseek. While it is expected that unpredictable behaviour may result while repositioning the offset of a file descriptor concurrently with reading/writing to the same file descriptor, this should not happen when merely *reading* the file descriptor's offset. Unfortunately, the only portable way in Unix to read a file descriptor's offset is lseek(fd, 0, SEEK_CUR); however executing this concurrently with read/write may mess up the position, as shown by the testcase below: #include #include #include #include #include #include #include void *loop(void *ptr) { fprintf(stderr, "Starting seek thread\n"); while(1) { if(lseek(0, 0LL, SEEK_CUR) < 0LL) perror("seek"); } } int main(int argc, char **argv) { long long l=0; int r; char buf[4096]; pthread_t thread; pthread_create(&thread, 0, loop, 0); for(r=0; 1 ; r++) { int n = read(0, buf, 4096); if(n == 0) break; if(n < 4096) { fprintf(stderr, "Short read %d %s\n", n, strerror(errno)); } l+= n; } fprintf(stderr, "Read %lld bytes\n", l); return 0; } Compile this and run it on a multi-processor machine as ./a.out f_pos) { file->f_pos = offset; file->f_version = 0; } However, this doesn't work if file->f_pos was changed (by read() or write()) between the time offset was computed, and the time where it considers writing it back. Signed-off-by: Alain Knaff Cc: Al Viro Cc: Christoph Hellwig Signed-off-by: Andrew Morton --- fs/read_write.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff -puN fs/read_write.c~vfs-lseekfd-0-seek_cur-race-condition fs/read_write.c --- a/fs/read_write.c~vfs-lseekfd-0-seek_cur-race-condition +++ a/fs/read_write.c @@ -50,6 +50,14 @@ generic_file_llseek_unlocked(struct file offset += inode->i_size; break; case SEEK_CUR: + /* + * Here we special-case the lseek(fd, 0, SEEK_CUR) + * position-querying operation. Avoid rewriting the "same" + * f_pos value back to the file because a concurrent read(), + * write() or lseek() might have altered it + */ + if (offset == 0) + return file->f_pos; offset += file->f_pos; break; } @@ -105,6 +113,14 @@ loff_t default_llseek(struct file *file, offset += i_size_read(file->f_path.dentry->d_inode); break; case SEEK_CUR: + /* + * See SEEK_CUR description in + * generic_file_llseek_unlocked() + */ + if (offset == 0) { + retval = file->f_pos; + goto out; + } offset += file->f_pos; } retval = -EINVAL; @@ -115,6 +131,7 @@ loff_t default_llseek(struct file *file, } retval = offset; } +out: unlock_kernel(); return retval; } _