Module Name:    othersrc
Committed By:   agc
Date:           Wed Jun 21 23:48:08 UTC 2023

Modified Files:
        othersrc/external/bsd/agcre/dist: agcre.c agcre.h
        othersrc/external/bsd/agcre/dist/tests: 62.expected

Log Message:
agcre version 20230621
======================

+ agcre - added internal magic numbers to agcre to attempt to catch
  if misbehaving programs overwrite sections of memory
+ agcre - check internal magic numbers before attempting to execute
  regex programs
+ agcre - bump agcre magic number in the external structure
+ agcre - bump version number in header file. Fix up tests to ensure
  correct operation


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 othersrc/external/bsd/agcre/dist/agcre.c
cvs rdiff -u -r1.5 -r1.6 othersrc/external/bsd/agcre/dist/agcre.h
cvs rdiff -u -r1.3 -r1.4 othersrc/external/bsd/agcre/dist/tests/62.expected

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/external/bsd/agcre/dist/agcre.c
diff -u othersrc/external/bsd/agcre/dist/agcre.c:1.3 othersrc/external/bsd/agcre/dist/agcre.c:1.4
--- othersrc/external/bsd/agcre/dist/agcre.c:1.3	Fri Feb 24 19:01:10 2023
+++ othersrc/external/bsd/agcre/dist/agcre.c	Wed Jun 21 23:48:08 2023
@@ -159,17 +159,26 @@ typedef struct threadlist_t {
 	re_thread_t	t[1];			/* the threads */
 } threadlist_t;
 
+#define MAGIC1		0xac1deaf0
+#define MAGIC2		0x41525345
+#define MAGIC3		0xd0d0d00d
+#define MAGIC4		0x666f7572
+
 /* regular expression internals */
 typedef struct re_t {
+	uint32_t	 magic1;	/* magic number #1 */
 	instr_t		*prog;		/* start of instructions */
 	uint32_t	 instrc;	/* # of instructions */
 	uint32_t	 gen;		/* generation number */
 	uint32_t	 setc;		/* # of sets */
 	uint32_t	 maxset;	/* allocated # of sets */
+	uint32_t	 magic2;	/* magic number #2 */
 	set_t		*sets;		/* sets */
 	uint32_t	 flags;		/* comp/exec flags */
 	context_t	*ctxlist;	/* list of contexts */
+	uint32_t	 magic3;	/* magic number #3 */
 	instr_t		*pc;		/* prog counter */
+	uint32_t	 magic4;	/* magic number #4 */
 	int		 msgc;		/* # of chars in msg buffer */
 	char		 msg[256];	/* message buffer */
 } re_t;
@@ -2669,6 +2678,22 @@ growspace(char **buf, size_t *size, size
 	return 1;
 }
 
+/* check it was compiled properly */
+static inline int
+good_struct(const agcre_regex_t *agcre)
+{
+	re_t	*re;
+
+	if (agcre == NULL || agcre->re_magic != AGCRE_MAGIC2) {
+		return 0;
+	}
+	if ((re = agcre->re_g) == NULL) {
+		return 0;
+	}
+	return re->magic1 = MAGIC1 && re->magic2 == MAGIC2 &&
+		re->magic3 == MAGIC3 && re->magic4 == MAGIC4;
+}
+
 /***********************************************************/
 
 /* allocate a new structure and return it */
@@ -2697,7 +2722,13 @@ agcre_regcomp(agcre_regex_t *agcre, cons
 		(agcre_regoff_t)(agcre->re_endp - in.s) :
 		(agcre_regoff_t)strlen(in.s);
 	memset(agcre, 0x0, sizeof(*agcre));
-	agcre->re_g = re = in.re = calloc(1, sizeof(*re));
+	if ((agcre->re_g = re = in.re = calloc(1, sizeof(*re))) == NULL) {
+		return AGCRE_REG_FAILURE;
+	}
+	re->magic1 = MAGIC1;
+	re->magic2 = MAGIC2;
+	re->magic3 = MAGIC3;
+	re->magic4 = MAGIC4;
 	if (in.eo - in.so > AGCRE_MAX_EXPR_LENGTH) {
 		re->msgc = snprintf(re->msg, sizeof(re->msg),
 			"expression length %llu larger than %u",
@@ -2739,7 +2770,7 @@ agcre_regcomp(agcre_regex_t *agcre, cons
 	re->pc->op = OP_MATCH;
 	re->pc += 1;
 	re->instrc = re->pc - re->prog;
-	agcre->re_magic = AGCRE_MAGIC;
+	agcre->re_magic = AGCRE_MAGIC2;
 	if (flags & AGCRE_REG_DUMP) {
 		printprog(re);
 	}
@@ -2757,7 +2788,7 @@ agcre_regerror(int errcode, const agcre_
 	re_t	*re;
 
 	USE_ARG(errcode);
-	if (agcre == NULL || size == 0 || errbuf == NULL) {
+	if (!good_struct(agcre) || size == 0 || errbuf == NULL) {
 		return 0;
 	}
 	re = agcre->re_g;
@@ -2782,15 +2813,15 @@ agcre_regexec(agcre_regex_t *agcre, cons
 	re_t		*re;
 	int		 ret;
 	
-	if (agcre == NULL || vs == NULL || (matchc > 0 && m == NULL)) {
+	if (!good_struct(agcre) || vs == NULL || (matchc > 0 && m == NULL)) {
 		return AGCRE_REG_FAILURE;
 	}
 	if ((re = agcre->re_g) == NULL) {
 		return AGCRE_REG_FAILURE;
 	}
-	if (agcre->re_magic != AGCRE_MAGIC) {
+	if (agcre->re_magic != AGCRE_MAGIC2) {
 		re->msgc = snprintf(re->msg, sizeof(re->msg),
-			"bad magic number 0x%x, not 0x%x", agcre->re_magic, AGCRE_MAGIC);
+			"bad magic number 0x%x, not 0x%x", agcre->re_magic, AGCRE_MAGIC2);
 		return AGCRE_REG_FAILURE;
 	}
 	if (matchc > AGCRE_MAX_SUBEXPR) {
@@ -2942,6 +2973,9 @@ agcre_rev_regexec(agcre_regex_t *agcre, 
 	int		found;
 	int		lastmatch;
 
+	if (!good_struct(agcre) || vs == NULL || (matchc > 0 && m == NULL)) {
+		return AGCRE_REG_FAILURE;
+	}
 	if (flags & AGCRE_REG_STARTEND) {
 		from = m[0].rm_so;
 		to = m[0].rm_eo;
@@ -2984,7 +3018,7 @@ agcre_regfree(agcre_regex_t *agcre)
 	uint32_t	 i;
 	re_t		*re;
 
-	if (agcre) {
+	if (agcre && good_struct(agcre)) {
 		if ((re = agcre->re_g) != NULL) {
 			free(re->prog);
 			for (i = 0 ; i < re->setc ; i++) {

Index: othersrc/external/bsd/agcre/dist/agcre.h
diff -u othersrc/external/bsd/agcre/dist/agcre.h:1.5 othersrc/external/bsd/agcre/dist/agcre.h:1.6
--- othersrc/external/bsd/agcre/dist/agcre.h:1.5	Fri Feb 24 19:01:10 2023
+++ othersrc/external/bsd/agcre/dist/agcre.h	Wed Jun 21 23:48:08 2023
@@ -29,7 +29,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #ifndef AGCRE_VERSION
-#define AGCRE_VERSION	20230224
+#define AGCRE_VERSION	20230621
 
 #include <inttypes.h>
 
@@ -53,7 +53,7 @@
 #define REG_SUCCESS		AGCRE_REG_SUCCESS
 #define REG_FAILURE		AGCRE_REG_FAILURE
 #define REG_NOMATCH		AGCRE_REG_FAILURE
-#define REG_MAGIC		AGCRE_MAGIC
+#define REG_MAGIC		AGCRE_MAGIC2
 #define REG_MAX_SUBEXPR		AGCRE_MAX_SUBEXPR
 #define REG_MAX_EXPR_LENGTH	AGCRE_MAX_EXPR_LENGTH
 #define REG_ANCHOR		AGCRE_REG_ANCHOR
@@ -89,7 +89,7 @@
 #define AGCRE_REG_SUCCESS	0
 #define AGCRE_REG_FAILURE	1
 
-#define AGCRE_MAGIC		0x20170801
+#define AGCRE_MAGIC2		0x20230621
 
 /* limits we impose on expressions */
 #define AGCRE_MAX_SUBEXPR	100

Index: othersrc/external/bsd/agcre/dist/tests/62.expected
diff -u othersrc/external/bsd/agcre/dist/tests/62.expected:1.3 othersrc/external/bsd/agcre/dist/tests/62.expected:1.4
--- othersrc/external/bsd/agcre/dist/tests/62.expected:1.3	Fri Feb 24 19:01:11 2023
+++ othersrc/external/bsd/agcre/dist/tests/62.expected	Wed Jun 21 23:48:08 2023
@@ -1 +1 @@
-agcre 20230224
+agcre 20230621

Reply via email to