Reputation:
I pass the argument Arturo $1$salt$ and I get a Segmentation fault (core dumped) error.
#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main (int argc, char* argv[])
{
if ( argc != 3) {
printf ("Usage: ./crypt key salt\n");
return 1;
}
printf ("%s\n", crypt (argv[1], argv[2]));
return 0;
}
Upvotes: 0
Views: 1165
Reputation: 5796
The answer turned out to be simple - bash variable expansion. the $
character is reserved in the shell, to mark the beginning of a variable
so when you run
./program Arturo $1$salt$
the argv[2]
after variable expansion will be
"$"
which is not a valid salt after the glibc specification (which expects $id$salt$
). The call to crypt
with that seed will return NULL
and set errno
to EINVAL
, because the seed is invalid, and the call to printf
chokes on the NULL
and segfaults, which is the behaviour you are seeing.
if you were to execute your program as follows, disabling variable expansion in the shell:
./program Arturo '$1$salt$'
the output would be
$1$salt$y5SOwLketmwNfSvW0yAoz/
as expected.
Upvotes: 2
Reputation: 145899
Assuming:
typedef char *string;
From crypt
POSIX documentation:
The salt argument shall be a string of at least two bytes in length not including the null character chosen from the set:
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 . /
but you pass $1$salt$
.
So you have to check the return value of crypt
and print only if is ! = NULL
.
Now, if you want to use crypt
function from glibc, you have to include crypt.h
header in your program. See example in glibc documentation.
Upvotes: 0