"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/utils.c" between
cryptsetup-2.3.6.tar.xz and cryptsetup-2.4.0.tar.xz

About: cryptsetup is a utility used to conveniently setup disk encryption based on the dm-crypt kernel module. These include plain dm-crypt volumes, LUKS volumes, loop-AES and TrueCrypt compatible format.

utils.c  (cryptsetup-2.3.6.tar.xz):utils.c  (cryptsetup-2.4.0.tar.xz)
skipping to change at line 174 skipping to change at line 174
*key_size_read = 0; *key_size_read = 0;
fd = keyfile ? open(keyfile, O_RDONLY) : STDIN_FILENO; fd = keyfile ? open(keyfile, O_RDONLY) : STDIN_FILENO;
if (fd < 0) { if (fd < 0) {
log_err(cd, _("Failed to open key file.")); log_err(cd, _("Failed to open key file."));
return -EINVAL; return -EINVAL;
} }
if (isatty(fd)) { if (isatty(fd)) {
log_err(cd, _("Cannot read keyfile from a terminal.")); log_err(cd, _("Cannot read keyfile from a terminal."));
r = -EINVAL; goto out;
goto out_err;
} }
/* If not requested otherwise, we limit input to prevent memory exhaustio n */ /* If not requested otherwise, we limit input to prevent memory exhaustio n */
if (key_size == 0) { if (key_size == 0) {
key_size = DEFAULT_KEYFILE_SIZE_MAXKB * 1024 + 1; key_size = DEFAULT_KEYFILE_SIZE_MAXKB * 1024 + 1;
unlimited_read = 1; unlimited_read = 1;
/* use 4k for buffer (page divisor but avoid huge pages) */ /* use 4k for buffer (page divisor but avoid huge pages) */
buflen = 4096 - sizeof(size_t); // sizeof(struct safe_allocation) ; buflen = 4096 - sizeof(size_t); // sizeof(struct safe_allocation) ;
} else } else
buflen = key_size; buflen = key_size;
regular_file = 0; regular_file = 0;
if (keyfile) { if (keyfile) {
if (stat(keyfile, &st) < 0) { if (stat(keyfile, &st) < 0) {
log_err(cd, _("Failed to stat key file.")); log_err(cd, _("Failed to stat key file."));
goto out_err; goto out;
} }
if (S_ISREG(st.st_mode)) { if (S_ISREG(st.st_mode)) {
regular_file = 1; regular_file = 1;
file_read_size = (uint64_t)st.st_size; file_read_size = (uint64_t)st.st_size;
if (keyfile_offset > file_read_size) { if (keyfile_offset > file_read_size) {
log_err(cd, _("Cannot seek to requested keyfile o ffset.")); log_err(cd, _("Cannot seek to requested keyfile o ffset."));
goto out_err; goto out;
} }
file_read_size -= keyfile_offset; file_read_size -= keyfile_offset;
/* known keyfile size, alloc it in one step */ /* known keyfile size, alloc it in one step */
if (file_read_size >= (uint64_t)key_size) if (file_read_size >= (uint64_t)key_size)
buflen = key_size; buflen = key_size;
else if (file_read_size) else if (file_read_size)
buflen = file_read_size; buflen = file_read_size;
} }
} }
pass = crypt_safe_alloc(buflen); pass = crypt_safe_alloc(buflen);
if (!pass) { if (!pass) {
log_err(cd, _("Out of memory while reading passphrase.")); log_err(cd, _("Out of memory while reading passphrase."));
goto out_err; goto out;
} }
/* Discard keyfile_offset bytes on input */ /* Discard keyfile_offset bytes on input */
if (keyfile_offset && keyfile_seek(fd, keyfile_offset) < 0) { if (keyfile_offset && keyfile_seek(fd, keyfile_offset) < 0) {
log_err(cd, _("Cannot seek to requested keyfile offset.")); log_err(cd, _("Cannot seek to requested keyfile offset."));
goto out_err; goto out;
} }
for (i = 0, newline = 0; i < key_size; i += char_read) { for (i = 0, newline = 0; i < key_size; i += char_read) {
if (i == buflen) { if (i == buflen) {
buflen += 4096; buflen += 4096;
pass = crypt_safe_realloc(pass, buflen); pass = crypt_safe_realloc(pass, buflen);
if (!pass) { if (!pass) {
log_err(cd, _("Out of memory while reading passph rase.")); log_err(cd, _("Out of memory while reading passph rase."));
r = -ENOMEM; r = -ENOMEM;
goto out_err; goto out;
} }
} }
if (flags & CRYPT_KEYFILE_STOP_EOL) { if (flags & CRYPT_KEYFILE_STOP_EOL) {
/* If we should stop on newline, we must read the input /* If we should stop on newline, we must read the input
* one character at the time. Otherwise we might end up * one character at the time. Otherwise we might end up
* having read some bytes after the newline, which we * having read some bytes after the newline, which we
* promised not to do. * promised not to do.
*/ */
char_to_read = 1; char_to_read = 1;
} else { } else {
/* char_to_read = min(key_size - i, buflen - i) */ /* char_to_read = min(key_size - i, buflen - i) */
char_to_read = key_size < buflen ? char_to_read = key_size < buflen ?
key_size - i : buflen - i; key_size - i : buflen - i;
} }
char_read = read_buffer(fd, &pass[i], char_to_read); char_read = read_buffer(fd, &pass[i], char_to_read);
if (char_read < 0) { if (char_read < 0) {
log_err(cd, _("Error reading passphrase.")); log_err(cd, _("Error reading passphrase."));
r = -EPIPE; r = -EPIPE;
goto out_err; goto out;
} }
if (char_read == 0) if (char_read == 0)
break; break;
/* Stop on newline only if not requested read from keyfile */ /* Stop on newline only if not requested read from keyfile */
if ((flags & CRYPT_KEYFILE_STOP_EOL) && pass[i] == '\n') { if ((flags & CRYPT_KEYFILE_STOP_EOL) && pass[i] == '\n') {
newline = 1; newline = 1;
pass[i] = '\0'; pass[i] = '\0';
break; break;
} }
} }
/* Fail if piped input dies reading nothing */ /* Fail if piped input dies reading nothing */
if (!i && !regular_file && !newline) { if (!i && !regular_file && !newline) {
log_err(cd, _("Nothing to read on input.")); log_err(cd, _("Nothing to read on input."));
r = -EPIPE; r = -EPIPE;
goto out_err; goto out;
} }
/* Fail if we exceeded internal default (no specified size) */ /* Fail if we exceeded internal default (no specified size) */
if (unlimited_read && i == key_size) { if (unlimited_read && i == key_size) {
log_err(cd, _("Maximum keyfile size exceeded.")); log_err(cd, _("Maximum keyfile size exceeded."));
goto out_err; goto out;
} }
if (!unlimited_read && i != key_size) { if (!unlimited_read && i != key_size) {
log_err(cd, _("Cannot read requested amount of data.")); log_err(cd, _("Cannot read requested amount of data."));
goto out_err; goto out;
} }
*key = pass; *key = pass;
*key_size_read = i; *key_size_read = i;
r = 0; r = 0;
out_err: out:
if (fd != STDIN_FILENO) if (fd != STDIN_FILENO)
close(fd); close(fd);
if (r) if (r)
crypt_safe_free(pass); crypt_safe_free(pass);
return r; return r;
} }
int crypt_keyfile_read(struct crypt_device *cd, const char *keyfile, int crypt_keyfile_read(struct crypt_device *cd, const char *keyfile,
char **key, size_t *key_size_read, char **key, size_t *key_size_read,
skipping to change at line 321 skipping to change at line 320
if (sscanf(uts.release, "%" SCNu16 ".%" SCNu16 ".%" SCNu16 "-%" SCNu16, if (sscanf(uts.release, "%" SCNu16 ".%" SCNu16 ".%" SCNu16 "-%" SCNu16,
&maj, &min, &patch, &rel) == 4) &maj, &min, &patch, &rel) == 4)
r = 0; r = 0;
else if (sscanf(uts.release, "%" SCNu16 ".%" SCNu16 ".%" SCNu16, else if (sscanf(uts.release, "%" SCNu16 ".%" SCNu16 ".%" SCNu16,
&maj, &min, &patch) == 3) { &maj, &min, &patch) == 3) {
rel = 0; rel = 0;
r = 0; r = 0;
} }
if (!r) if (!r)
*kversion = version(maj, min, patch, rel); *kversion = compact_version(maj, min, patch, rel);
return r; return r;
} }
bool crypt_string_in(const char *str, char **list, size_t list_size) bool crypt_string_in(const char *str, char **list, size_t list_size)
{ {
size_t i; size_t i;
for (i = 0; *list && i < list_size; i++, list++) for (i = 0; *list && i < list_size; i++, list++)
if (!strcmp(str, *list)) if (!strcmp(str, *list))
 End of changes. 12 change blocks. 
13 lines changed or deleted 12 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)