Skip to content

Commit

Permalink
Fix oss-fuzz 67175, 67180, 67191
Browse files Browse the repository at this point in the history
Embedded 0 characters in a file would prevent the loading of that file.

This could not happen apart from corruption of the file, or by
deliberate manipulation by the admin. Minimal impact.
  • Loading branch information
ralight committed Mar 3, 2024
1 parent 2762a87 commit 14c3fbe
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 24 deletions.
6 changes: 5 additions & 1 deletion apps/mosquitto_passwd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ OBJS_EXTERNAL= \
memory_mosq.o \
memory_public.o \
misc_mosq.o \
password_mosq.o
password_mosq.o \
utf8_mosq.o


ifeq ($(WITH_TLS),yes)
Expand Down Expand Up @@ -54,6 +55,9 @@ misc_mosq.o : ${R}/common/misc_mosq.c ${R}/common/misc_mosq.h
password_mosq.o : ${R}/common/password_mosq.c ${R}/common/password_mosq.h
${CROSS_COMPILE}${CC} ${LOCAL_CPPFLAGS} $(LOCAL_CFLAGS) -c $< -o $@

utf8_mosq.o : ${R}/common/utf8_mosq.c
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@

install : all
ifeq ($(WITH_TLS),yes)
$(INSTALL) -d "${DESTDIR}$(prefix)/bin"
Expand Down
40 changes: 24 additions & 16 deletions common/misc_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,38 +260,46 @@ char *misc__trimblanks(char *str)

char *fgets_extending(char **buf, int *buflen, FILE *stream)
{
char *rc;
char endchar;
int offset = 0;
char *newbuf;
size_t len;

if(stream == NULL || buf == NULL || buflen == NULL || *buflen < 1){
if(stream == NULL || buf == NULL || buflen == NULL || *buflen < 1 || feof(stream)){
return NULL;
}
memset(*buf, 0, (size_t)(*buflen));

do{
rc = fgets(&((*buf)[offset]), (*buflen)-offset, stream);
if(feof(stream) || rc == NULL){
return rc;
}
while(offset < (*buflen-1)){
int c = fgetc(stream);

len = strlen(*buf);
if(len == 0){
return rc;
}
endchar = (*buf)[len-1];
if(endchar == '\n'){
return rc;
if(c == EOF){
if(mosquitto_validate_utf8(*buf, offset) == MOSQ_ERR_SUCCESS){
return *buf;
}else{
return NULL;
}
}

if(c == '\n'){
if(mosquitto_validate_utf8(*buf, offset) == MOSQ_ERR_SUCCESS){
return *buf;
}else{
return NULL;
}
}else{
(*buf)[offset] = (char)c;
offset++;
}
}

/* No EOL char found, so extend buffer */
offset = (*buflen)-1;
*buflen += 1000;
newbuf = realloc(*buf, (size_t)*buflen);
if(!newbuf){
return NULL;
}
*buf = newbuf;
memset(&(*buf[offset]), 0, 1000);
}while(1);
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ set(C_SRC
thread_mosq.c
../common/time_mosq.c ../common/time_mosq.h
tls_mosq.c
utf8_mosq.c
../common/utf8_mosq.c
util_mosq.c util_topic.c util_mosq.h
will_mosq.c will_mosq.h)

Expand Down
7 changes: 5 additions & 2 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ OBJS= \
strings_mosq.o \
thread_mosq.o \
tls_mosq.o \
utf8_mosq.o \
util_mosq.o \
util_topic.o \
will_mosq.o
Expand All @@ -90,7 +89,8 @@ OBJS_EXTERNAL= \
base64_mosq.o \
misc_mosq.o \
password_mosq.o \
time_mosq.o
time_mosq.o \
utf8_mosq.o

ifeq ($(WITH_WEBSOCKETS),yes)
OBJS_EXTERNAL+=${R}/deps/picohttpparser/picohttpparser.o
Expand Down Expand Up @@ -160,5 +160,8 @@ password_mosq.o : ${R}/common/password_mosq.c net_mosq.h
time_mosq.o : ${R}/common/time_mosq.c ${R}/common/time_mosq.h
${CROSS_COMPILE}$(CC) $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@

utf8_mosq.o : ${R}/common/utf8_mosq.c
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@

${R}/deps/picohttpparser/picohttpparser.o : ${R}/deps/picohttpparser/picohttpparser.c
${CROSS_COMPILE}$(CC) $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ set (MOSQ_SRCS
../lib/tls_mosq.c
topic_tok.c
../lib/util_mosq.c ../lib/util_topic.c ../lib/util_mosq.h
../lib/utf8_mosq.c
../common/utf8_mosq.c
websockets.c
will_delay.c
../lib/will_mosq.c ../lib/will_mosq.h
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ util_mosq.o : ${R}/lib/util_mosq.c ${R}/lib/util_mosq.h
util_topic.o : ${R}/lib/util_topic.c ${R}/lib/util_mosq.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@

utf8_mosq.o : ${R}/lib/utf8_mosq.c
utf8_mosq.o : ${R}/common/utf8_mosq.c
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@

will_mosq.o : ${R}/lib/will_mosq.c ${R}/lib/will_mosq.h
Expand Down
2 changes: 1 addition & 1 deletion test/unit/broker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ ${R}/src/util_mosq.o : ${R}/lib/util_mosq.c
${R}/src/util_topic.o : ${R}/lib/util_topic.c
$(MAKE) -C ${R}/src/ util_topic.o

${R}/src/utf8_mosq.o : ${R}/lib/utf8_mosq.c
${R}/src/utf8_mosq.o : ${R}/common/utf8_mosq.c
$(MAKE) -C ${R}/src/ utf8_mosq.o

build : bridge_topic_test keepalive_test persist_read_test persist_write_test subs_test
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ${R}/lib/util_mosq.o : ${R}/lib/util_mosq.c
${R}/lib/util_topic.o : ${R}/lib/util_topic.c
$(MAKE) -C ${R}/lib/ util_topic.o

${R}/lib/utf8_mosq.o : ${R}/lib/utf8_mosq.c
${R}/lib/utf8_mosq.o : ${R}/common/utf8_mosq.c
$(MAKE) -C ${R}/lib/ utf8_mosq.o

build : lib_test
Expand Down

0 comments on commit 14c3fbe

Please sign in to comment.