[PATCH] Finish the task 'Validate coredump format string'

Tiwei Bie btw at mail.ustc.edu.cn
Sat Mar 21 13:59:42 UTC 2015


Hi, Mateusz!

I have finished the task: Validate coredump format string [1].

Following is my patch:

---
 sys/kern/kern_sig.c | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c
index 8410d9d..52f05be 100644
--- a/sys/kern/kern_sig.c
+++ b/sys/kern/kern_sig.c
@@ -3099,13 +3099,38 @@ static char corefilename[MAXPATHLEN] = {"%N.core"};
 static int
 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
 {
-	int error;
+	char *format;
+	int i, error;
+
+	format = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
+
+	sx_slock(&corefilename_lock);
+	strncpy(format, corefilename, MAXPATHLEN);
+	sx_sunlock(&corefilename_lock);
+
+	error = sysctl_handle_string(oidp, format, MAXPATHLEN, req);
+	if (error != 0 || strcmp(format, corefilename) == 0)
+		goto out;
+
+	for (i = 0; format[i] != '\0'; i++) {
+		if (format[i] == '%') {
+			char ch = format[++i];
+			if (ch != '%' && ch != 'H' && ch != 'I' &&
+			    ch != 'N' && ch != 'P' && ch != 'U') {
+				error = EINVAL;
+				printf("Unknown format character %c in "
+				    "corename `%s'\n", ch, format);
+				goto out;
+			}
+		}
+	}
 
 	sx_xlock(&corefilename_lock);
-	error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
-	    req);
+	strncpy(corefilename, format, sizeof(corefilename));
 	sx_xunlock(&corefilename_lock);
 
+out:
+	free(format, M_TEMP);
 	return (error);
 }
 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RWTUN |
-- 
2.1.2

[1] https://wiki.freebsd.org/JuniorJobs#Validate_coredump_format_string

Best regards,
Tiwei Bie



More information about the freebsd-hackers mailing list