eternaltyro / cryptsetup

Since Google code is shuttering...
http://code.google.com/p/cryptsetup
GNU General Public License v2.0
0 stars 0 forks source link

Segfault due to use of non-portable strerror_r #237

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. build cryptsetup with musl libc
2. run: cryptsetup -y -v luksFormat /path/to/tempfile
3. on the question "Are you sure? (Type uppercase yes):" type anything but YES. 
Simply <enter> will trigger it.

What is the expected output? What do you see instead?

Expected: Command failed with code 22: Invalid argument

What happens: Segfault

What version of the product are you using? On what operating system?

cryptsetup-1.6.6 on Alpine Linux (which uses musl libc)

Please provide any additional information below.

reported here: http://bugs.alpinelinux.org/issues/3470

backtrace:
Core was generated by `cryptsetup -y -v luksFormat /home/ncopa/test/test.img'.
Program terminated with signal 11, Segmentation fault.
#0  0x00006c0f7f877c1b in __stpncpy (
    d=d@entry=0x77da45f183b8 "Invalid argument", s=0x0, n=n@entry=256)
    at src/string/stpncpy.c:18
18    src/string/stpncpy.c: No such file or directory.
(gdb) bt
#0  0x00006c0f7f877c1b in __stpncpy (
    d=d@entry=0x77da45f183b8 "Invalid argument", s=0x0, n=n@entry=256)
    at src/string/stpncpy.c:18
#1  0x00006c0f7f8781c1 in strncpy (
    d=d@entry=0x77da45f183b8 "Invalid argument", s=<optimized out>, 
    n=n@entry=256) at src/string/strncpy.c:7
#2  0x000006ec125723be in show_status (errcode=-22) at utils_tools.c:184
#3  0x000006ec12570c48 in run_action (
    action=0x6ec12779218 <action_types+280>) at cryptsetup.c:1425
#4  main (argc=5, argv=0x77da45f185e8) at cryptsetup.c:1698

Original issue reported on code.google.com by natanael...@gmail.com on 17 Dec 2014 at 8:35

GoogleCodeExporter commented 9 years ago
The problem is that cryptsetup assumes GNU behavior of strerror_r:

int strerror_r(int errnum, char *buf, size_t buflen);
            /* XSI-compliant */

char *strerror_r(int errnum, char *buf, size_t buflen);
            /* GNU-specific */

While musl only implement the XSI-compliant version.

Original comment by natanael...@gmail.com on 17 Dec 2014 at 8:38

GoogleCodeExporter commented 9 years ago
Possible fix:
diff --git a/src/utils_tools.c b/src/utils_tools.c
index 4e8b0b4..1b4f3e5 100644
--- a/src/utils_tools.c
+++ b/src/utils_tools.c
@@ -176,11 +176,18 @@ void show_status(int errcode)
        crypt_get_error(error, sizeof(error));

        if (!error[0]) {
+#if defined(__GLIBC__)
+               /* GNU libc strerror_r is non-portable. */
                error_ = strerror_r(-errcode, error, sizeof(error));
                if (error_ != error) {
                        strncpy(error, error_, sizeof(error));
                        error[sizeof(error) - 1] = '\0';
                }
+#else
+               /* POSIX variant */
+               if (strerror_r(-errcode, error, sizeof(error)) != 0)
+                       error[0] = '\0';
+#endif
        }

        log_err(_("Command failed with code %i"), -errcode);

Original comment by natanael...@gmail.com on 18 Dec 2014 at 1:08

GoogleCodeExporter commented 9 years ago
Yes, we will need some wrapper for this, thanks.

Will fix this in next release.

Original comment by gmazyl...@gmail.com on 18 Dec 2014 at 4:22

GoogleCodeExporter commented 9 years ago
Fix (using autoconf macro) committed in
https://code.google.com/p/cryptsetup/source/detail?r=e24a72f84ca996787169a0128b5
c560e3548aac0

Thanks!

Original comment by gmazyl...@gmail.com on 10 Jan 2015 at 7:40