summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrwatson <rwatson@FreeBSD.org>2002-02-18 00:37:03 +0000
committerrwatson <rwatson@FreeBSD.org>2002-02-18 00:37:03 +0000
commit49c65a6b10085c64f7ff4dfb8ccc792f1e9dbc75 (patch)
treeca54ff259a2aa28bbc146e5a0c0d691cd98ede9f
parent43f97fe4ae07b305a42ebbd3f668b9fdeecbaa5f (diff)
downloadFreeBSD-src-49c65a6b10085c64f7ff4dfb8ccc792f1e9dbc75.zip
FreeBSD-src-49c65a6b10085c64f7ff4dfb8ccc792f1e9dbc75.tar.gz
Add a 'strvalid()' call to libkern. Given a character pointer, and
buffer length, determine if the pointer is to a valid string. Currently, the only check is whether a '\0' appears in the buffer. This is useful when pulling in a structure from userland that may contain one or more strings, and validity testing must be performed on elements of the structure. When copying normal string arguments, copyinstr() is expected to be used.
-rw-r--r--sys/conf/files1
-rw-r--r--sys/libkern/strvalid.c55
-rw-r--r--sys/sys/libkern.h1
3 files changed, 57 insertions, 0 deletions
diff --git a/sys/conf/files b/sys/conf/files
index c662a06..d96f2d6 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -906,6 +906,7 @@ libkern/strtol.c standard
libkern/strtoq.c standard
libkern/strtoul.c standard
libkern/strtouq.c standard
+libkern/strvalid.c standard
net/bpf.c standard
net/bpf_filter.c optional bpf
bpf.h standard \
diff --git a/sys/libkern/strvalid.c b/sys/libkern/strvalid.c
new file mode 100644
index 0000000..df1b071
--- /dev/null
+++ b/sys/libkern/strvalid.c
@@ -0,0 +1,55 @@
+/*-
+ * Copyright (c) 2002 Networks Associates Technologies, Inc.
+ * All rights reserved.
+ *
+ * This software was developed for the FreeBSD Project by NAI Labs,
+ * the Security Research Division of Network Associates, Inc. under
+ * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
+ * CHATS research program.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The names of the authors may not be used to endorse or promote
+ * products derived from this software without specific prior written
+ * permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/types.h>
+#include <sys/libkern.h>
+
+/*
+ * Return (1) if the buffer pointed to by kernel pointer 'buffer' and
+ * of length 'bufferlen' contains a valid NUL-terminated string
+ */
+int
+strvalid(const char *buffer, size_t bufferlen)
+{
+ int i;
+
+ /* Must be NUL-terminated. */
+ for (i = 0; i < bufferlen; i++)
+ if (buffer[i] == '\0')
+ return (1);
+
+ return (0);
+}
diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h
index ccaf7e7..5049d3e 100644
--- a/sys/sys/libkern.h
+++ b/sys/sys/libkern.h
@@ -89,6 +89,7 @@ char *strcpy __P((char *, const char *));
size_t strlen __P((const char *));
int strncmp __P((const char *, const char *, size_t));
char *strncpy __P((char *, const char *, size_t));
+int strvalid __P((const char *, size_t));
static __inline int
OpenPOWER on IntegriCloud