Skip to content

Commit 84baf74

Browse files
dhowellstorvalds
authored andcommitted
ROMFS: romfs_lookup() shouldn't be doing a partial name comparison
romfs_lookup() should be using a routine akin to strcmp() on the backing store, rather than one akin to strncmp(). If it uses the latter, it's liable to match /bin/shutdown when looking up /bin/sh. Signed-off-by: David Howells <dhowells@redhat.com> Tested-by: Michal Simek <monstr@monstr.eu> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a5422a5 commit 84baf74

3 files changed

Lines changed: 53 additions & 22 deletions

File tree

‎fs/romfs/internal.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ extern int romfs_dev_read(struct super_block *sb, unsigned long pos,
4343
void *buf, size_t buflen);
4444
extern ssize_t romfs_dev_strnlen(struct super_block *sb,
4545
unsigned long pos, size_t maxlen);
46-
extern int romfs_dev_strncmp(struct super_block *sb, unsigned long pos,
47-
const char *str, size_t size);
46+
extern int romfs_dev_strcmp(struct super_block *sb, unsigned long pos,
47+
const char *str, size_t size);

‎fs/romfs/storage.c‎

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,35 @@ static ssize_t romfs_mtd_strnlen(struct super_block *sb,
6767
* compare a string to one in a romfs image on MTD
6868
* - return 1 if matched, 0 if differ, -ve if error
6969
*/
70-
static int romfs_mtd_strncmp(struct super_block *sb, unsigned long pos,
71-
const char *str, size_t size)
70+
static int romfs_mtd_strcmp(struct super_block *sb, unsigned long pos,
71+
const char *str, size_t size)
7272
{
73-
u_char buf[16];
73+
u_char buf[17];
7474
size_t len, segment;
7575
int ret;
7676

77-
/* scan the string up to 16 bytes at a time */
77+
/* scan the string up to 16 bytes at a time, and attempt to grab the
78+
* trailing NUL whilst we're at it */
79+
buf[0] = 0xff;
80+
7881
while (size > 0) {
79-
segment = min_t(size_t, size, 16);
82+
segment = min_t(size_t, size + 1, 17);
8083
ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
8184
if (ret < 0)
8285
return ret;
86+
len--;
8387
if (memcmp(buf, str, len) != 0)
8488
return 0;
89+
buf[0] = buf[len];
8590
size -= len;
8691
pos += len;
8792
str += len;
8893
}
8994

95+
/* check the trailing NUL was */
96+
if (buf[0])
97+
return 0;
98+
9099
return 1;
91100
}
92101
#endif /* CONFIG_ROMFS_ON_MTD */
@@ -154,28 +163,48 @@ static ssize_t romfs_blk_strnlen(struct super_block *sb,
154163
* compare a string to one in a romfs image on a block device
155164
* - return 1 if matched, 0 if differ, -ve if error
156165
*/
157-
static int romfs_blk_strncmp(struct super_block *sb, unsigned long pos,
158-
const char *str, size_t size)
166+
static int romfs_blk_strcmp(struct super_block *sb, unsigned long pos,
167+
const char *str, size_t size)
159168
{
160169
struct buffer_head *bh;
161170
unsigned long offset;
162171
size_t segment;
163-
bool x;
172+
bool matched, terminated = false;
164173

165-
/* scan the string up to 16 bytes at a time */
174+
/* compare string up to a block at a time */
166175
while (size > 0) {
167176
offset = pos & (ROMBSIZE - 1);
168177
segment = min_t(size_t, size, ROMBSIZE - offset);
169178
bh = sb_bread(sb, pos >> ROMBSBITS);
170179
if (!bh)
171180
return -EIO;
172-
x = (memcmp(bh->b_data + offset, str, segment) != 0);
173-
brelse(bh);
174-
if (x)
175-
return 0;
181+
matched = (memcmp(bh->b_data + offset, str, segment) == 0);
182+
176183
size -= segment;
177184
pos += segment;
178185
str += segment;
186+
if (matched && size == 0 && offset + segment < ROMBSIZE) {
187+
if (!bh->b_data[offset + segment])
188+
terminated = true;
189+
else
190+
matched = false;
191+
}
192+
brelse(bh);
193+
if (!matched)
194+
return 0;
195+
}
196+
197+
if (!terminated) {
198+
/* the terminating NUL must be on the first byte of the next
199+
* block */
200+
BUG_ON((pos & (ROMBSIZE - 1)) != 0);
201+
bh = sb_bread(sb, pos >> ROMBSBITS);
202+
if (!bh)
203+
return -EIO;
204+
matched = !bh->b_data[0];
205+
brelse(bh);
206+
if (!matched)
207+
return 0;
179208
}
180209

181210
return 1;
@@ -234,10 +263,12 @@ ssize_t romfs_dev_strnlen(struct super_block *sb,
234263

235264
/*
236265
* compare a string to one in romfs
266+
* - the string to be compared to, str, may not be NUL-terminated; instead the
267+
* string is of the specified size
237268
* - return 1 if matched, 0 if differ, -ve if error
238269
*/
239-
int romfs_dev_strncmp(struct super_block *sb, unsigned long pos,
240-
const char *str, size_t size)
270+
int romfs_dev_strcmp(struct super_block *sb, unsigned long pos,
271+
const char *str, size_t size)
241272
{
242273
size_t limit;
243274

@@ -246,16 +277,16 @@ int romfs_dev_strncmp(struct super_block *sb, unsigned long pos,
246277
return -EIO;
247278
if (size > ROMFS_MAXFN)
248279
return -ENAMETOOLONG;
249-
if (size > limit - pos)
280+
if (size + 1 > limit - pos)
250281
return -EIO;
251282

252283
#ifdef CONFIG_ROMFS_ON_MTD
253284
if (sb->s_mtd)
254-
return romfs_mtd_strncmp(sb, pos, str, size);
285+
return romfs_mtd_strcmp(sb, pos, str, size);
255286
#endif
256287
#ifdef CONFIG_ROMFS_ON_BLOCK
257288
if (sb->s_bdev)
258-
return romfs_blk_strncmp(sb, pos, str, size);
289+
return romfs_blk_strcmp(sb, pos, str, size);
259290
#endif
260291
return -EIO;
261292
}

‎fs/romfs/super.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ static struct dentry *romfs_lookup(struct inode *dir, struct dentry *dentry,
240240
goto error;
241241

242242
/* try to match the first 16 bytes of name */
243-
ret = romfs_dev_strncmp(dir->i_sb, offset + ROMFH_SIZE, name,
244-
len);
243+
ret = romfs_dev_strcmp(dir->i_sb, offset + ROMFH_SIZE, name,
244+
len);
245245
if (ret < 0)
246246
goto error;
247247
if (ret == 1)

0 commit comments

Comments
 (0)