Skip to content

Commit 0e5cc9a

Browse files
committed
udf: Check path length when reading symlink
Symlink reading code does not check whether the resulting path fits into the page provided by the generic code. This isn't as easy as just checking the symlink size because of various encoding conversions we perform on path. So we have to check whether there is still enough space in the buffer on the fly. CC: stable@vger.kernel.org Reported-by: Carl Henrik Lunde <chlunde@ping.uio.no> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent a1d47b2 commit 0e5cc9a

5 files changed

Lines changed: 48 additions & 20 deletions

File tree

‎fs/udf/dir.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
167167
continue;
168168
}
169169

170-
flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
170+
flen = udf_get_filename(dir->i_sb, nameptr, lfi, fname,
171+
UDF_NAME_LEN);
171172
if (!flen)
172173
continue;
173174

‎fs/udf/namei.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ static struct fileIdentDesc *udf_find_entry(struct inode *dir,
233233
if (!lfi)
234234
continue;
235235

236-
flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
236+
flen = udf_get_filename(dir->i_sb, nameptr, lfi, fname,
237+
UDF_NAME_LEN);
237238
if (flen && udf_match(flen, fname, child->len, child->name))
238239
goto out_ok;
239240
}

‎fs/udf/symlink.c‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
#include <linux/buffer_head.h>
3131
#include "udf_i.h"
3232

33-
static void udf_pc_to_char(struct super_block *sb, unsigned char *from,
34-
int fromlen, unsigned char *to)
33+
static int udf_pc_to_char(struct super_block *sb, unsigned char *from,
34+
int fromlen, unsigned char *to, int tolen)
3535
{
3636
struct pathComponent *pc;
3737
int elen = 0;
38+
int comp_len;
3839
unsigned char *p = to;
3940

41+
/* Reserve one byte for terminating \0 */
42+
tolen--;
4043
while (elen < fromlen) {
4144
pc = (struct pathComponent *)(from + elen);
4245
switch (pc->componentType) {
@@ -49,22 +52,37 @@ static void udf_pc_to_char(struct super_block *sb, unsigned char *from,
4952
break;
5053
/* Fall through */
5154
case 2:
55+
if (tolen == 0)
56+
return -ENAMETOOLONG;
5257
p = to;
5358
*p++ = '/';
59+
tolen--;
5460
break;
5561
case 3:
62+
if (tolen < 3)
63+
return -ENAMETOOLONG;
5664
memcpy(p, "../", 3);
5765
p += 3;
66+
tolen -= 3;
5867
break;
5968
case 4:
69+
if (tolen < 2)
70+
return -ENAMETOOLONG;
6071
memcpy(p, "./", 2);
6172
p += 2;
73+
tolen -= 2;
6274
/* that would be . - just ignore */
6375
break;
6476
case 5:
65-
p += udf_get_filename(sb, pc->componentIdent, p,
66-
pc->lengthComponentIdent);
77+
comp_len = udf_get_filename(sb, pc->componentIdent,
78+
pc->lengthComponentIdent,
79+
p, tolen);
80+
p += comp_len;
81+
tolen -= comp_len;
82+
if (tolen == 0)
83+
return -ENAMETOOLONG;
6784
*p++ = '/';
85+
tolen--;
6886
break;
6987
}
7088
elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
@@ -73,6 +91,7 @@ static void udf_pc_to_char(struct super_block *sb, unsigned char *from,
7391
p[-1] = '\0';
7492
else
7593
p[0] = '\0';
94+
return 0;
7695
}
7796

7897
static int udf_symlink_filler(struct file *file, struct page *page)
@@ -108,8 +127,10 @@ static int udf_symlink_filler(struct file *file, struct page *page)
108127
symlink = bh->b_data;
109128
}
110129

111-
udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p);
130+
err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
112131
brelse(bh);
132+
if (err)
133+
goto out_unlock_inode;
113134

114135
up_read(&iinfo->i_data_sem);
115136
SetPageUptodate(page);

‎fs/udf/udfdecl.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ udf_get_lb_pblock(struct super_block *sb, struct kernel_lb_addr *loc,
211211
}
212212

213213
/* unicode.c */
214-
extern int udf_get_filename(struct super_block *, uint8_t *, uint8_t *, int);
214+
extern int udf_get_filename(struct super_block *, uint8_t *, int, uint8_t *,
215+
int);
215216
extern int udf_put_filename(struct super_block *, const uint8_t *, uint8_t *,
216217
int);
217218
extern int udf_build_ustr(struct ustr *, dstring *, int);

‎fs/udf/unicode.c‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929
#include "udf_sb.h"
3030

31-
static int udf_translate_to_linux(uint8_t *, uint8_t *, int, uint8_t *, int);
31+
static int udf_translate_to_linux(uint8_t *, int, uint8_t *, int, uint8_t *,
32+
int);
3233

3334
static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
3435
{
@@ -333,8 +334,8 @@ static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
333334
return u_len + 1;
334335
}
335336

336-
int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
337-
int flen)
337+
int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
338+
uint8_t *dname, int dlen)
338339
{
339340
struct ustr *filename, *unifilename;
340341
int len = 0;
@@ -347,7 +348,7 @@ int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
347348
if (!unifilename)
348349
goto out1;
349350

350-
if (udf_build_ustr_exact(unifilename, sname, flen))
351+
if (udf_build_ustr_exact(unifilename, sname, slen))
351352
goto out2;
352353

353354
if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
@@ -366,7 +367,8 @@ int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
366367
} else
367368
goto out2;
368369

369-
len = udf_translate_to_linux(dname, filename->u_name, filename->u_len,
370+
len = udf_translate_to_linux(dname, dlen,
371+
filename->u_name, filename->u_len,
370372
unifilename->u_name, unifilename->u_len);
371373
out2:
372374
kfree(unifilename);
@@ -403,10 +405,12 @@ int udf_put_filename(struct super_block *sb, const uint8_t *sname,
403405
#define EXT_MARK '.'
404406
#define CRC_MARK '#'
405407
#define EXT_SIZE 5
408+
/* Number of chars we need to store generated CRC to make filename unique */
409+
#define CRC_LEN 5
406410

407-
static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
408-
int udfLen, uint8_t *fidName,
409-
int fidNameLen)
411+
static int udf_translate_to_linux(uint8_t *newName, int newLen,
412+
uint8_t *udfName, int udfLen,
413+
uint8_t *fidName, int fidNameLen)
410414
{
411415
int index, newIndex = 0, needsCRC = 0;
412416
int extIndex = 0, newExtIndex = 0, hasExt = 0;
@@ -439,7 +443,7 @@ static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
439443
newExtIndex = newIndex;
440444
}
441445
}
442-
if (newIndex < 256)
446+
if (newIndex < newLen)
443447
newName[newIndex++] = curr;
444448
else
445449
needsCRC = 1;
@@ -467,13 +471,13 @@ static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
467471
}
468472
ext[localExtIndex++] = curr;
469473
}
470-
maxFilenameLen = 250 - localExtIndex;
474+
maxFilenameLen = newLen - CRC_LEN - localExtIndex;
471475
if (newIndex > maxFilenameLen)
472476
newIndex = maxFilenameLen;
473477
else
474478
newIndex = newExtIndex;
475-
} else if (newIndex > 250)
476-
newIndex = 250;
479+
} else if (newIndex > newLen - CRC_LEN)
480+
newIndex = newLen - CRC_LEN;
477481
newName[newIndex++] = CRC_MARK;
478482
valueCRC = crc_itu_t(0, fidName, fidNameLen);
479483
newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);

0 commit comments

Comments
 (0)