Skip to content

Commit

Permalink
util: fix not checking return value of RAND_bytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsura committed Feb 8, 2024
1 parent 355a4c0 commit f3b5cd8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
16 changes: 13 additions & 3 deletions core/src/lib/util.cc
Expand Up @@ -45,6 +45,7 @@
#include <vector>

#include <openssl/rand.h>
#include <openssl/err.h>

// Various BAREOS Utility subroutines

Expand Down Expand Up @@ -692,11 +693,18 @@ int DoShellExpansion(char* name, int name_len)
#endif

/* Create a new session key. key needs to be able to hold at least
* 32 + 7 (separator) + 1 (null) = 40 bytes. */
void MakeSessionKey(char key[40])
* 120 bytes (keys are 40 bytes long, but errors might be longer).
* If successful, key contains the generated key, otherwise key will
* contain an error message. */
bool MakeSessionKey(char key[120])
{
unsigned char s[16];
RAND_bytes(s, sizeof(s));

if (RAND_bytes(s, sizeof(s)) != 1) {
auto err = ERR_get_error();
ERR_error_string(err, key);
return false;
}

for (int j = 0; j < 16; j++) {
char low = (s[j] & 0x0F);
Expand All @@ -708,6 +716,8 @@ void MakeSessionKey(char key[40])
if (j & 1) { *key++ = '-'; }
}
*--key = 0;

return true;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/util.h
Expand Up @@ -67,7 +67,7 @@ const char* job_type_to_str(int type);
const char* job_replace_to_str(int relace);
const char* job_status_to_str(int stat);
const char* job_level_to_str(int level);
void MakeSessionKey(char key[40]);
bool MakeSessionKey(char key[120]);
POOLMEM* edit_job_codes(JobControlRecord* jcr,
char* omsg,
const char* imsg,
Expand Down
12 changes: 10 additions & 2 deletions core/src/stored/job.cc
Expand Up @@ -154,7 +154,11 @@ bool job_cmd(JobControlRecord* jcr)
Dmsg1(50, "Quota set as %llu\n", quota);

// Pass back an authorization key for the File daemon
MakeSessionKey(auth_key);
if (!MakeSessionKey(auth_key)) {
Jmsg2(jcr, M_FATAL, 0, "Could not generate authentication key: %s.\n",
auth_key);
return false;
}
jcr->sd_auth_key = strdup(auth_key);
dir->fsend(OK_job, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
memset(auth_key, 0, sizeof(auth_key));
Expand Down Expand Up @@ -251,7 +255,11 @@ bool nextRunCmd(JobControlRecord* jcr)
jcr->authenticated = false;

// Pass back a new authorization key for the File daemon
MakeSessionKey(auth_key);
if (!MakeSessionKey(auth_key)) {
Jmsg1(jcr, M_FATAL, 0, "Could not generate authentication key: %s.\n",
auth_key);
return false;
}
if (jcr->sd_auth_key) { free(jcr->sd_auth_key); }
jcr->sd_auth_key = strdup(auth_key);
dir->fsend(OK_nextrun, auth_key);
Expand Down

0 comments on commit f3b5cd8

Please sign in to comment.