Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 21 Mar 2015 21:59:05 +0800
From:      Tiwei Bie <btw@mail.ustc.edu.cn>
To:        mjguzik@gmail.com
Cc:        freebsd-hackers@freebsd.org
Subject:   [PATCH] Finish the task 'Validate coredump format string'
Message-ID:  <1426946345-67889-1-git-send-email-btw@mail.ustc.edu.cn>

next in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1426946345-67889-1-git-send-email-btw>