Message ID | 20201223121536.6244-1-kechengsong@huawei.com |
---|---|
State | New |
Delegated to: | Richard Weinberger |
Headers | show |
Series | [v2] ubifs: Fix read out-of-bounds in ubifs_jnl_write_inode() | expand |
Chengsong Ke, ----- Ursprüngliche Mail ----- > The memory area allocated in ubifs_jnl_write_inode() is not aligned with 8 > bytes: > ino_start = ino = kmalloc(write_len, GFP_NOFS); > > When ino_start passed into write_head -> ubifs_wbuf_write_nolock: > n = aligned_len >> c->max_write_shift; > if (n) { > n <<= c->max_write_shift; > err = ubifs_leb_write(c, wbuf->lnum, buf + written, wbuf->offs, n); > // Read oob occurs here, read n bytes from buf, and buf is passed from > @ino_start which is > // not 8 bytes aligned(write_len < n). Program read (n - write_len) more bytes. > } > > Reproducer: > 0. config KASAN && apply print.patch > 1. mount ubifs on /root/temp > 2. run test.sh What does test.sh do? > 3. cd /root/temp && ls // change atime for link_file > 4. wait 1~2 minutes > > In order to solve the read oob problem in ubifs_wbuf_write_nolock, just align > the write_len to > 8 bytes when alloc the memory. So that this patch will not affect the use of > write_len in other > functions, such as ubifs_jnl_write_inode->make_reservation and > ubifs_jnl_write_inode->ubifs_node_calc_hash. I gave this a second thought and I'm not so sure anymore what exactly is going on. The problem is real, I fully agree with you but I need to dig deeper into the journal and wbuf code to double check that we really fix the right thing and not just paper other something. Thanks, //richard
在 2020/12/24 7:07, Richard Weinberger 写道: >> Reproducer: >> 0. config KASAN && apply print.patch >> 1. mount ubifs on /root/temp >> 2. run test.sh > > What does test.sh do? Go to Link: https://bugzilla.kernel.org/show_bug.cgi?id=210865. test.sh creates a very long path file test_file, and then create a symbol link link_file for test_file, so ubifs inode for link_file will be assigned a big value for ui->data_len. When we change atime for link_file, ubifs_jnl_write_inode will be executed by wb_writeback. By this way, write_len could be not aligned with 8 bytes. > >> 3. cd /root/temp && ls // change atime for link_file >> 4. wait 1~2 minutes >> >> In order to solve the read oob problem in ubifs_wbuf_write_nolock, just align >> the write_len to >> 8 bytes when alloc the memory. So that this patch will not affect the use of >> write_len in other >> functions, such as ubifs_jnl_write_inode->make_reservation and >> ubifs_jnl_write_inode->ubifs_node_calc_hash. > > I gave this a second thought and I'm not so sure anymore what exactly is going on. > The problem is real, I fully agree with you but I need to dig deeper into > the journal and wbuf code to double check that we really fix the right thing > and not just paper other something. > > Thanks, > //richard > . >
ping >> Reproducer: >> 0. config KASAN && apply print.patch >> 1. mount ubifs on /root/temp >> 2. run test.sh > > What does test.sh do? Go to Link: https://bugzilla.kernel.org/show_bug.cgi?id=210865. test.sh creates a very long path file test_file, and then create a symbol link link_file for test_file, so ubifs inode for link_file will be assigned a big value for ui->data_len. When we change atime for link_file, ubifs_jnl_write_inode will be executed by wb_writeback. By this way, write_len could be not aligned with 8 bytes. > >> 3. cd /root/temp && ls // change atime for link_file >> 4. wait 1~2 minutes >> >> In order to solve the read oob problem in ubifs_wbuf_write_nolock, just align >> the write_len to >> 8 bytes when alloc the memory. So that this patch will not affect the use of >> write_len in other >> functions, such as ubifs_jnl_write_inode->make_reservation and >> ubifs_jnl_write_inode->ubifs_node_calc_hash. > > I gave this a second thought and I'm not so sure anymore what exactly is going on. > The problem is real, I fully agree with you but I need to dig deeper into > the journal and wbuf code to double check that we really fix the right thing > and not just paper other something. > > Thanks, > //richard > . >
ping >> Reproducer: >> 0. config KASAN && apply print.patch >> 1. mount ubifs on /root/temp >> 2. run test.sh > > What does test.sh do? Go to Link: https://bugzilla.kernel.org/show_bug.cgi?id=210865. test.sh creates a very long path file test_file, and then create a symbol link link_file for test_file, so ubifs inode for link_file will be assigned a big value for ui->data_len. When we change atime for link_file, ubifs_jnl_write_inode will be executed by wb_writeback. By this way, write_len could be not aligned with 8 bytes. > >> 3. cd /root/temp && ls // change atime for link_file >> 4. wait 1~2 minutes >> >> In order to solve the read oob problem in ubifs_wbuf_write_nolock, just align >> the write_len to >> 8 bytes when alloc the memory. So that this patch will not affect the use of >> write_len in other >> functions, such as ubifs_jnl_write_inode->make_reservation and >> ubifs_jnl_write_inode->ubifs_node_calc_hash. > > I gave this a second thought and I'm not so sure anymore what exactly is going on. > The problem is real, I fully agree with you but I need to dig deeper into > the journal and wbuf code to double check that we really fix the right thing > and not just paper other something. > > Thanks, > //richard > . >
diff --git a/fs/ubifs/journal.c b/fs/ubifs/journal.c index 03410ae0813a..fc918a66d208 100644 --- a/fs/ubifs/journal.c +++ b/fs/ubifs/journal.c @@ -577,7 +577,7 @@ int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir, else len += host_ui->data_len; - dent = kzalloc(len, GFP_NOFS); + dent = kzalloc(ALIGN(len, 8), GFP_NOFS); if (!dent) return -ENOMEM; @@ -866,7 +866,7 @@ int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode) else write_len += ilen; - ino_start = ino = kmalloc(write_len, GFP_NOFS); + ino_start = ino = kzalloc(ALIGN(write_len, 8), GFP_NOFS); if (!ino) return -ENOMEM;