Skip to content

Commit

Permalink
Merge pull request #1169 from nebeid/upstream-merge-2023-08-25
Browse files Browse the repository at this point in the history
Upstream merge 2023 08 25
  • Loading branch information
nebeid authored Sep 5, 2023
2 parents aa90e50 + 24009be commit fe308e3
Show file tree
Hide file tree
Showing 40 changed files with 1,304 additions and 1,140 deletions.
213 changes: 28 additions & 185 deletions crypto/conf/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,10 @@
#include "conf_def.h"
#include "internal.h"
#include "../internal.h"
#include "../lhash/internal.h"


DEFINE_LHASH_OF(CONF_VALUE)

struct conf_st {
LHASH_OF(CONF_VALUE) *data;
};

static const char kDefaultSectionName[] = "default";

// The maximum length we can grow a value to after variable expansion. 64k
// should be more than enough for all reasonable uses.
#define MAX_CONF_VALUE_LENGTH 65536

static uint32_t conf_value_hash(const CONF_VALUE *v) {
const uint32_t section_hash = v->section ? OPENSSL_strhash(v->section) : 0;
const uint32_t name_hash = v->name ? OPENSSL_strhash(v->name) : 0;
Expand Down Expand Up @@ -139,24 +128,23 @@ CONF_VALUE *CONF_VALUE_new(void) {
}

static void value_free_contents(CONF_VALUE *value) {
if (value->section) {
OPENSSL_free(value->section);
}
OPENSSL_free(value->section);
if (value->name) {
OPENSSL_free(value->name);
if (value->value) {
OPENSSL_free(value->value);
}
OPENSSL_free(value->value);
} else {
if (value->value) {
sk_CONF_VALUE_free((STACK_OF(CONF_VALUE)*)value->value);
}
// TODO(davidben): When |value->name| is NULL, |CONF_VALUE| is actually an
// entirely different structure. This is fragile and confusing. Make a
// proper |CONF_SECTION| type that doesn't require this.
sk_CONF_VALUE_free((STACK_OF(CONF_VALUE) *)value->value);
}
}

static void value_free(CONF_VALUE *value) {
value_free_contents(value);
OPENSSL_free(value);
if (value != NULL) {
value_free_contents(value);
OPENSSL_free(value);
}
}

static void value_free_arg(CONF_VALUE *value, void *arg) { value_free(value); }
Expand Down Expand Up @@ -192,28 +180,21 @@ static CONF_VALUE *NCONF_new_section(const CONF *conf, const char *section) {
if (!lh_CONF_VALUE_insert(conf->data, &old_value, v)) {
goto err;
}
if (old_value) {
value_free(old_value);
}
value_free(old_value);
ok = 1;

err:
if (!ok) {
if (sk != NULL) {
sk_CONF_VALUE_free(sk);
}
if (v != NULL) {
OPENSSL_free(v);
}
sk_CONF_VALUE_free(sk);
OPENSSL_free(v);
v = NULL;
}
return v;
}

static int str_copy(CONF *conf, char *section, char **pto, char *from) {
int q, r, rr = 0, to = 0, len = 0;
char *s, *e, *rp, *rrp, *np, *cp, v;
const char *p;
int q, to = 0, len = 0;
char v;
BUF_MEM *buf;

buf = BUF_MEM_new();
Expand Down Expand Up @@ -242,22 +223,6 @@ static int str_copy(CONF *conf, char *section, char **pto, char *from) {
if (*from == q) {
from++;
}
} else if (IS_DQUOTE(conf, *from)) {
q = *from;
from++;
while (!IS_EOF(conf, *from)) {
if (*from == q) {
if (*(from + 1) == q) {
from++;
} else {
break;
}
}
buf->data[to++] = *(from++);
}
if (*from == q) {
from++;
}
} else if (IS_ESC(conf, *from)) {
from++;
v = *(from++);
Expand All @@ -276,102 +241,23 @@ static int str_copy(CONF *conf, char *section, char **pto, char *from) {
} else if (IS_EOF(conf, *from)) {
break;
} else if (*from == '$') {
// try to expand it
rrp = NULL;
s = &(from[1]);
if (*s == '{') {
q = '}';
} else if (*s == '(') {
q = ')';
} else {
q = 0;
}

if (q) {
s++;
}
cp = section;
e = np = s;
while (IS_ALPHA_NUMERIC(conf, *e)) {
e++;
}
if (e[0] == ':' && e[1] == ':') {
cp = np;
rrp = e;
rr = *e;
*rrp = '\0';
e += 2;
np = e;
while (IS_ALPHA_NUMERIC(conf, *e)) {
e++;
}
}
r = *e;
*e = '\0';
rp = e;
if (q) {
if (r != q) {
OPENSSL_PUT_ERROR(CONF, CONF_R_NO_CLOSE_BRACE);
goto err;
}
e++;
}
// So at this point we have
// np which is the start of the name string which is
// '\0' terminated.
// cp which is the start of the section string which is
// '\0' terminated.
// e is the 'next point after'.
// r and rr are the chars replaced by the '\0'
// rp and rrp is where 'r' and 'rr' came from.
p = NCONF_get_string(conf, cp, np);
if (rrp != NULL) {
*rrp = rr;
}
*rp = r;
if (p == NULL) {
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
goto err;
}
size_t newsize = strlen(p) + buf->length - (e - from);
if (newsize > MAX_CONF_VALUE_LENGTH) {
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
goto err;
}
if (!BUF_MEM_grow_clean(buf, newsize)) {
goto err;
}
while (*p) {
buf->data[to++] = *(p++);
}

/* Since we change the pointer 'from', we also have
to change the perceived length of the string it
points at. /RL */
len -= e - from;
from = e;

/* In case there were no braces or parenthesis around
the variable reference, we have to put back the
character that was replaced with a '\0'. /RL */
*rp = r;
// Historically, $foo would expand to a previously-parsed value. This
// feature has been removed as it was unused and is a DoS vector.
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_NOT_SUPPORTED);
goto err;
} else {
buf->data[to++] = *(from++);
}
}

buf->data[to] = '\0';
if (*pto != NULL) {
OPENSSL_free(*pto);
}
OPENSSL_free(*pto);
*pto = buf->data;
OPENSSL_free(buf);
return 1;

err:
if (buf != NULL) {
BUF_MEM_free(buf);
}
BUF_MEM_free(buf);
return 0;
}

Expand Down Expand Up @@ -472,33 +358,8 @@ static char *scan_quote(CONF *conf, char *p) {
return p;
}


static char *scan_dquote(CONF *conf, char *p) {
int q = *p;

p++;
while (!(IS_EOF(conf, *p))) {
if (*p == q) {
if (*(p + 1) == q) {
p++;
} else {
break;
}
}
p++;
}
if (*p == q) {
p++;
}
return p;
}

static void clear_comments(CONF *conf, char *p) {
for (;;) {
if (IS_FCOMMENT(conf, *p)) {
*p = '\0';
return;
}
if (!IS_WS(conf, *p)) {
break;
}
Expand All @@ -510,10 +371,6 @@ static void clear_comments(CONF *conf, char *p) {
*p = '\0';
return;
}
if (IS_DQUOTE(conf, *p)) {
p = scan_dquote(conf, p);
continue;
}
if (IS_QUOTE(conf, *p)) {
p = scan_quote(conf, p);
continue;
Expand Down Expand Up @@ -707,37 +564,23 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
v = NULL;
}
}
if (buff != NULL) {
BUF_MEM_free(buff);
}
if (section != NULL) {
OPENSSL_free(section);
}
BUF_MEM_free(buff);
OPENSSL_free(section);
return 1;

err:
if (buff != NULL) {
BUF_MEM_free(buff);
}
if (section != NULL) {
OPENSSL_free(section);
}
BUF_MEM_free(buff);
OPENSSL_free(section);
if (out_error_line != NULL) {
*out_error_line = eline;
}
BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
ERR_add_error_data(2, "line ", btmp);

if (v != NULL) {
if (v->name != NULL) {
OPENSSL_free(v->name);
}
if (v->value != NULL) {
OPENSSL_free(v->value);
}
if (v != NULL) {
OPENSSL_free(v);
}
OPENSSL_free(v->name);
OPENSSL_free(v->value);
OPENSSL_free(v);
}
return 0;
}
Expand Down
10 changes: 3 additions & 7 deletions crypto/conf/conf_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
* [including the GNU Public Licence.]
*/

/* THIS FILE WAS AUTOMAGICALLY GENERATED!
Please modify and use keysets.pl to regenerate it. */
// This file was historically generated by keysets.pl in OpenSSL.
//
// TODO(davidben): Replace it with something more readable.

#define CONF_NUMBER 1
#define CONF_UPPER 2
Expand All @@ -66,9 +67,7 @@
#define CONF_WS 16
#define CONF_ESC 32
#define CONF_QUOTE 64
#define CONF_DQUOTE 1024
#define CONF_COMMENT 128
#define CONF_FCOMMENT 2048
#define CONF_EOF 8
#define CONF_HIGHBIT 4096
#define CONF_ALPHA (CONF_UPPER|CONF_LOWER)
Expand All @@ -78,7 +77,6 @@

#define KEYTYPES(c) CONF_type_default
#define IS_COMMENT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_COMMENT)
#define IS_FCOMMENT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_FCOMMENT)
#define IS_EOF(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_EOF)
#define IS_ESC(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_ESC)
#define IS_NUMBER(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_NUMBER)
Expand All @@ -87,8 +85,6 @@
#define IS_ALPHA_NUMERIC_PUNCT(c,a) \
(KEYTYPES(c)[(a)&0xff]&CONF_ALPHA_NUMERIC_PUNCT)
#define IS_QUOTE(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_QUOTE)
#define IS_DQUOTE(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_DQUOTE)
#define IS_HIGHBIT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_HIGHBIT)

static const unsigned short CONF_type_default[256]={
0x0008,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
Expand Down
Loading

0 comments on commit fe308e3

Please sign in to comment.