diff options
author | Earl Chew <echew@ixiacom.com> | 2012-03-21 19:33:43 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-03-21 20:54:54 -0400 |
commit | 7904ac84244b59f536c2a5d1066a10f46df07b08 (patch) | |
tree | 82d9c44c26b52954884a70f1ae2b7fa78ea8e633 /fs | |
parent | dc716e96f5a467835e8121e1caaf25d66a901cb3 (diff) |
seq_file: fix mishandling of consecutive pread() invocations.
The following program illustrates the problem:
char buf[8192];
int fd = open("/proc/self/maps", O_RDONLY);
n = pread(fd, buf, sizeof(buf), 0);
printf("%d\n", n);
/* lseek(fd, 0, SEEK_CUR); */ /* Uncomment to work around */
n = pread(fd, buf, sizeof(buf), 0);
printf("%d\n", n);
The second printf() prints zero, but uncommenting the lseek() corrects its
behaviour.
To fix, make seq_read() mirror seq_lseek() when processing changes in
*ppos. Restore m->version first, then if required traverse and update
read_pos on success.
Addresses https://bugzilla.kernel.org/show_bug.cgi?id=11856
Signed-off-by: Earl Chew <echew@ixiacom.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/seq_file.c | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/fs/seq_file.c b/fs/seq_file.c index 4023d6be939b..aa242dc99373 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c | |||
@@ -140,9 +140,21 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) | |||
140 | 140 | ||
141 | mutex_lock(&m->lock); | 141 | mutex_lock(&m->lock); |
142 | 142 | ||
143 | /* | ||
144 | * seq_file->op->..m_start/m_stop/m_next may do special actions | ||
145 | * or optimisations based on the file->f_version, so we want to | ||
146 | * pass the file->f_version to those methods. | ||
147 | * | ||
148 | * seq_file->version is just copy of f_version, and seq_file | ||
149 | * methods can treat it simply as file version. | ||
150 | * It is copied in first and copied out after all operations. | ||
151 | * It is convenient to have it as part of structure to avoid the | ||
152 | * need of passing another argument to all the seq_file methods. | ||
153 | */ | ||
154 | m->version = file->f_version; | ||
155 | |||
143 | /* Don't assume *ppos is where we left it */ | 156 | /* Don't assume *ppos is where we left it */ |
144 | if (unlikely(*ppos != m->read_pos)) { | 157 | if (unlikely(*ppos != m->read_pos)) { |
145 | m->read_pos = *ppos; | ||
146 | while ((err = traverse(m, *ppos)) == -EAGAIN) | 158 | while ((err = traverse(m, *ppos)) == -EAGAIN) |
147 | ; | 159 | ; |
148 | if (err) { | 160 | if (err) { |
@@ -152,21 +164,11 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) | |||
152 | m->index = 0; | 164 | m->index = 0; |
153 | m->count = 0; | 165 | m->count = 0; |
154 | goto Done; | 166 | goto Done; |
167 | } else { | ||
168 | m->read_pos = *ppos; | ||
155 | } | 169 | } |
156 | } | 170 | } |
157 | 171 | ||
158 | /* | ||
159 | * seq_file->op->..m_start/m_stop/m_next may do special actions | ||
160 | * or optimisations based on the file->f_version, so we want to | ||
161 | * pass the file->f_version to those methods. | ||
162 | * | ||
163 | * seq_file->version is just copy of f_version, and seq_file | ||
164 | * methods can treat it simply as file version. | ||
165 | * It is copied in first and copied out after all operations. | ||
166 | * It is convenient to have it as part of structure to avoid the | ||
167 | * need of passing another argument to all the seq_file methods. | ||
168 | */ | ||
169 | m->version = file->f_version; | ||
170 | /* grab buffer if we didn't have one */ | 172 | /* grab buffer if we didn't have one */ |
171 | if (!m->buf) { | 173 | if (!m->buf) { |
172 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); | 174 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); |