Skip to contentSkip to conversation

btrfs-progs: fix segfault when listing column OTIME on big endian host

PATCH Superseded Eryu Guan 4 participants 6 messages Jul 25, 2013 – Aug 1, 2013 Btrfs file system

lore
Share & tools
lore

Eryu Guan

author
#0 lore raw

PATCH 1/1 btrfs-progs: fix segfault when listing column OTIME on big endian host

The second btrfs command segfaults on big endian host(ppc64)

btrfs subvolume snapshot /mnt/btrfs /mnt/btrfs/snap btrfs subvolume list -s /mnt/btrfs

And ltrace shows

localtime(0x10029c482d0) = 0
strftime( <no return ...>
--- SIGSEGV (Segmentation fault) ---

The corresponding code

btrfs-list.c:

        case BTRFS_LIST_OTIME:
                if (subv->otime)
                        strftime(tstr, 256, "%Y-%m-%d %X",
                                 localtime(&subv->otime));
                else
                        strcpy(tstr, "-");
                printf("%s", tstr);
                break;

localtime() returned NULL then strftime() got SIGSEGV.

The reason is that ri->otime.sec is stored as little endian but assigned to 't' without conversion.

---
 btrfs-list.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
btrfs-list.c +1 -1
index 4fab858..ca1bae8 100644
@@ -1052,7 +1052,7 @@ static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
 				flags = btrfs_root_flags(ri);
 				if(sh.len >
 				   sizeof(struct btrfs_root_item_v0)) {
-					t = ri->otime.sec;
+					t = le64_to_cpu(ri->otime.sec);
 					ogen = btrfs_root_otransid(ri);
 					memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
 					memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 38 lines · Eryu Guan

The second btrfs command segfaults on big endian host(ppc64)

btrfs subvolume snapshot /mnt/btrfs /mnt/btrfs/snap btrfs subvolume list -s /mnt/btrfs

And ltrace shows

localtime(0x10029c482d0) = 0
strftime( <no return ...>
--- SIGSEGV (Segmentation fault) ---

The corresponding code

btrfs-list.c:

        case BTRFS_LIST_OTIME:
                if (subv->otime)
                        strftime(tstr, 256, "%Y-%m-%d %X",
                                 localtime(&subv->otime));
                else
                        strcpy(tstr, "-");
                printf("%s", tstr);
                break;

localtime() returned NULL then strftime() got SIGSEGV.

The reason is that ri->otime.sec is stored as little endian but assigned to 't' without conversion.

---
 btrfs-list.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
btrfs-list.c +0 -0
index 4fab858..ca1bae8 100644

btrfs-list.c static int __list_subvol_search(int fd, struct root_lookup *root_lookup) · line 1055

Quoted 5 lines
btrfs-list.c +1 -1
@@ -1052,7 +1052,7 @@ static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
 				flags = btrfs_root_flags(ri);
 				if(sh.len >
 				   sizeof(struct btrfs_root_item_v0)) {
-					t = ri->otime.sec;
+					t = le64_to_cpu(ri->otime.sec);

It is better to use btrfs_stack_timespec_sec() instead of raw convert.

Thanks
Miao

Quoted 4 lines
btrfs-list.c +0 -0
 					ogen = btrfs_root_otransid(ri);
 					memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
 					memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
author
#2 lore raw

PATCH v2 1/1 btrfs-progs: fix segfault when listing column OTIME on big endian host

The second btrfs command segfaults on big endian host(ppc64)

btrfs subvolume snapshot /mnt/btrfs /mnt/btrfs/snap btrfs subvolume list -s /mnt/btrfs

And ltrace shows

localtime(0x10029c482d0) = 0
strftime( <no return ...>
--- SIGSEGV (Segmentation fault) ---

The corresponding code

btrfs-list.c:

        case BTRFS_LIST_OTIME:
                if (subv->otime)
                        strftime(tstr, 256, "%Y-%m-%d %X",
                                 localtime(&subv->otime));
                else
                        strcpy(tstr, "-");
                printf("%s", tstr);
                break;

localtime() returned NULL then strftime() got SIGSEGV.

The reason is that ri->otime.sec is stored as little endian but assigned to 't' without conversion.

---
v1->v2:
use btrfs_stack_timespec_sec() instead of raw convert. Thanks Miao Xie <[email protected]> for pointing this out.

 btrfs-list.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
btrfs-list.c +1 -1
index 4fab858..9deddd5 100644
@@ -1052,7 +1052,7 @@ static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
 				flags = btrfs_root_flags(ri);
 				if(sh.len >
 				   sizeof(struct btrfs_root_item_v0)) {
-					t = ri->otime.sec;
+					t = btrfs_stack_timespec_sec(&ri->otime);
 					ogen = btrfs_root_otransid(ri);
 					memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
 					memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 10 lines

btrfs-list.c:

        case BTRFS_LIST_OTIME:
                if (subv->otime)
                        strftime(tstr, 256, "%Y-%m-%d %X",
                                 localtime(&subv->otime));
                else
                        strcpy(tstr, "-");
                printf("%s", tstr);
                break;
Quoted 4 lines

localtime() returned NULL then strftime() got SIGSEGV.

The reason is that ri->otime.sec is stored as little endian but assigned to 't' without conversion.

That's why localtime() returned null, sure, but it doesn't excuse strftime() being called with a null *tm! Add some error checking around localtime(). It should warn that otime is nonsense, not crash.

- z

author
#4 lore raw
Quoted 10 lines · Zach Brown
Quoted 10 lines

btrfs-list.c:

        case BTRFS_LIST_OTIME:
                if (subv->otime)
                        strftime(tstr, 256, "%Y-%m-%d %X",
                                 localtime(&subv->otime));
                else
                        strcpy(tstr, "-");
                printf("%s", tstr);
                break;
Quoted 9 lines
Quoted 4 lines

localtime() returned NULL then strftime() got SIGSEGV.

The reason is that ri->otime.sec is stored as little endian but assigned to 't' without conversion.

That's why localtime() returned null, sure, but it doesn't excuse strftime() being called with a null *tm! Add some error checking around localtime(). It should warn that otime is nonsense, not crash.

Yes, return value of localtime() should be checked. There're other places call localtime() or localtime_r() without checking the return value, I think another patch could fix them all and leave this patch to fix the root cause.

Thanks,
Eryu Guan

#5 lore raw
In reply to Eryu Guan
Quoted 23 lines · Eryu Guan

The second btrfs command segfaults on big endian host(ppc64)

btrfs subvolume snapshot /mnt/btrfs /mnt/btrfs/snap btrfs subvolume list -s /mnt/btrfs

And ltrace shows

localtime(0x10029c482d0) = 0
strftime( <no return ...>
--- SIGSEGV (Segmentation fault) ---

The corresponding code

btrfs-list.c:

        case BTRFS_LIST_OTIME:
                if (subv->otime)
                        strftime(tstr, 256, "%Y-%m-%d %X",
                                 localtime(&subv->otime));
                else
                        strcpy(tstr, "-");
                printf("%s", tstr);
                break;
Quoted 4 lines

localtime() returned NULL then strftime() got SIGSEGV.

The reason is that ri->otime.sec is stored as little endian but assigned to 't' without conversion.

I've sent fixes to both otime endianity and localtime crashes some days ago and will keep them in integration branch. The reentrant variant of localtime is safer, though the fix to otime access might be enough to fix it.

http://www.mail-archive.com/[email protected]/msg25676.html
http://www.mail-archive.com/[email protected]/msg25677.html

david