Date: Wed, 9 May 2001 01:30:16 -0700 From: Kris Kennaway <kris@obsecurity.org> To: audit@FreeBSD.org, jkh@FreeBSD.org Subject: pkg_add patch Message-ID: <20010509013016.A29331@xor.obsecurity.org>
next in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
This is part I of a patch to pkg_*. I started out doing this, then
somehow got sidetracked into making all of pkg_install/*
BDECFLAGS-clean (343 warnings!! I'm down to about 50, half of which
are harmless/impossible to get rid of, the others I'm not immediately
sure how to fix. I want to review the changes I've made before I send
them out, so perhaps tomorrow).
The rest of pkg_* needs a good cleaning up too, but I don't have the
energy right now.
This patch fixes the string buffer ops in pkg_add/main.c and tidies up
the handling of the package version directory selection.
Kris
Index: add/main.c
===================================================================
RCS file: /mnt/ncvs/src/usr.sbin/pkg_install/add/main.c,v
retrieving revision 1.35
diff -u -r1.35 main.c
--- add/main.c 2001/04/26 06:48:59 1.35
+++ add/main.c 2001/05/09 08:18:37
@@ -49,6 +49,23 @@
char pkgnames[MAX_PKGS][MAXPATHLEN];
char *pkgs[MAX_PKGS];
+struct {
+ int lowver; /* Lowest version number to match */
+ int hiver; /* Highest version number to match */
+ const char *directory; /* Directory it lives in */
+} releases[] = {
+ { 410000, 410000, "/packages-4.1-release" },
+ { 420000, 420000, "/packages-4.2-release" },
+ { 430000, 430000, "/packages-4.3-release" },
+ { 440000, 440000, "/packages-4.4-release" },
+ { 450000, 450000, "/packages-4.5-release" },
+ { 300000, 399000, "/packages-3-stable" },
+ { 400000, 499000, "/packages-4-stable" },
+ { 510000, 599000, "/packages-5-stable" },
+ { 0, 9999999, "/packages-current" },
+ { 0, 0, NULL }
+};
+
static char *getpackagesite(void);
int getosreldate(void);
@@ -57,11 +74,9 @@
int
main(int argc, char **argv)
{
- int ch, err;
+ int ch, error;
char **start;
- char *cp;
-
- char *remotepkg = NULL, *ptr;
+ char *cp, *packagesite, *remotepkg = NULL, *ptr;
static char temppackageroot[MAXPATHLEN];
start = argv;
@@ -97,7 +112,8 @@
break;
case 't':
- strcpy(FirstPen, optarg);
+ if (s_strlcpy(FirstPen, optarg, sizeof(FirstPen)))
+ errx(1, "-t Argument too long.");
break;
case 'S':
@@ -119,8 +135,7 @@
argv += optind;
if (argc > MAX_PKGS) {
- warnx("too many packages (max %d)", MAX_PKGS);
- return(1);
+ errx(1, "too many packages (max %d)", MAX_PKGS);
}
if (AddMode != SLAVE) {
@@ -129,26 +144,42 @@
/* Get all the remaining package names, if any */
for (ch = 0; *argv; ch++, argv++) {
if (Remote) {
- strcpy(temppackageroot, getpackagesite());
- remotepkg = strcat(temppackageroot, *argv);
+ if ((packagesite = getpackagesite()) == NULL)
+ errx(1, "package name too long");
+ if (s_strlcpy(temppackageroot, packagesite,
+ sizeof(temppackageroot)))
+ errx(1, "package name too long");
+ if (s_strlcat(temppackageroot, *argv,
+ sizeof(temppackageroot)))
+ errx(1, "package name too long");
+ remotepkg = temppackageroot;
if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
ptr[2] == 'g' && ptr[3] == 'z' && !ptr[4]))
- strcat(remotepkg, ".tgz");
+ if (s_strlcat(remotepkg, ".tgz", sizeof(temppackageroot)))
+ errx(1, "package name too long");
}
if (!strcmp(*argv, "-")) /* stdin? */
pkgs[ch] = "-";
- else if (isURL(*argv)) /* preserve URLs */
- pkgs[ch] = strcpy(pkgnames[ch], *argv);
- else if ((Remote) && isURL(remotepkg))
- pkgs[ch] = strcpy(pkgnames[ch], remotepkg);
- else { /* expand all pathnames to fullnames */
+ else if (isURL(*argv)) { /* preserve URLs */
+ if (s_strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch])))
+ errx(1, "package name too long");
+ pkgs[ch] = pkgnames[ch];
+ }
+ else if ((Remote) && isURL(remotepkg)) {
+ if (s_strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch])))
+ errx(1, "package name too long");
+ pkgs[ch] = pkgnames[ch];
+ } else { /* expand all pathnames to fullnames */
if (fexists(*argv)) /* refers to a file directly */
pkgs[ch] = realpath(*argv, pkgnames[ch]);
else { /* look for the file in the expected places */
if (!(cp = fileFindByPath(NULL, *argv)))
warnx("can't find package '%s'", *argv);
- else
- pkgs[ch] = strcpy(pkgnames[ch], cp);
+ else {
+ if (s_strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch])))
+ errx(1, "package name too long");
+ pkgs[ch] = pkgnames[ch];
+ }
}
}
}
@@ -170,10 +201,10 @@
/* Set a reasonable umask */
umask(022);
- if ((err = pkg_perform(pkgs)) != 0) {
+ if ((error = pkg_perform(pkgs)) != 0) {
if (Verbose)
- warnx("%d package addition(s) failed", err);
- return err;
+ warnx("%d package addition(s) failed", error);
+ return error;
}
else
return 0;
@@ -182,51 +213,43 @@
static char *
getpackagesite(void)
{
- int reldate;
+ int reldate, i;
static char sitepath[MAXPATHLEN];
struct utsname u;
if (getenv("PACKAGESITE")) {
- strcpy(sitepath, getenv("PACKAGESITE"));
+ if (s_strlcpy(sitepath, getenv("PACKAGESITE"),
+ sizeof(sitepath)))
+ return NULL;
return sitepath;
}
-
- if (getenv("PACKAGEMIRROR"))
- strcpy(sitepath, getenv("PACKAGEMIRROR"));
- else
- strcpy(sitepath, "ftp://ftp.FreeBSD.org");
- if (getenv("PACKAGEROOT"))
- strcpy(sitepath, getenv("PACKAGEMIRRORROOT"));
- else
- strcat(sitepath, "/pub");
+ if (getenv("PACKAGEROOT")) {
+ if (s_strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath)))
+ return NULL;
+ } else {
+ if (s_strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath)))
+ return NULL;
+ }
- strcat(sitepath, "/FreeBSD/ports/");
+ if (s_strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath)))
+ return NULL;
uname(&u);
- strcat(sitepath, u.machine);
+ if (s_strlcat(sitepath, u.machine, sizeof(sitepath)))
+ return NULL;
reldate = getosreldate();
- if (reldate == 410000)
- strcat(sitepath, "/packages-4.1-release");
- else if (reldate == 420000)
- strcat(sitepath, "/packages-4.2-release");
- else if (reldate == 430000)
- strcat(sitepath, "/packages-4.3-release");
- else if (reldate == 440000)
- strcat(sitepath, "/packages-4.4-release");
- else if (reldate == 450000)
- strcat(sitepath, "/packages-4.5-release");
- else if (300000 <= reldate && reldate <= 399000)
- strcat(sitepath, "/packages-3-stable");
- else if (400000 <= reldate && reldate <= 499000)
- strcat(sitepath, "/packages-4-stable");
- else if (510000 <= reldate && reldate <= 599000) /* get real values!! */
- strcat(sitepath, "/packages-5-stable");
- else
- strcat(sitepath, "/packages-current");
+ for(i = 0; releases[i].directory != NULL; i++) {
+ if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
+ if (s_strlcat(sitepath, releases[i].directory, sizeof(sitepath)))
+ return NULL;
+ continue;
+ }
+ }
- strcat(sitepath, "/Latest/");
+ if (s_strlcat(sitepath, "/Latest/", sizeof(sitepath)))
+ return NULL;
return sitepath;
Index: lib/lib.h
===================================================================
RCS file: /mnt/ncvs/src/usr.sbin/pkg_install/lib/lib.h,v
retrieving revision 1.34
diff -u -r1.34 lib.h
--- lib/lib.h 2001/03/23 18:45:24 1.34
+++ lib/lib.h 2001/05/09 08:17:02
@@ -128,6 +128,8 @@
char *basename_of(char *);
char *strconcat(char *, char *);
char *get_string(char *, int, FILE *);
+int s_strlcpy(char *, const char *, size_t);
+int s_strlcat(char *, const char *, size_t);
/* File */
Boolean fexists(char *);
Index: lib/str.c
===================================================================
RCS file: /mnt/ncvs/src/usr.sbin/pkg_install/lib/str.c,v
retrieving revision 1.8
diff -u -r1.8 str.c
--- lib/str.c 2001/03/23 18:45:24 1.8
+++ lib/str.c 2001/05/09 08:16:03
@@ -61,6 +61,20 @@
return *str;
}
+/* Do a strlcpy and test for overflow */
+int
+s_strlcpy(char *dst, const char *src, size_t size)
+{
+ return (strlcpy(dst, src, size) >= size);
+}
+
+/* Do a strlcat and test for overflow */
+int
+s_strlcat(char *dst, const char *src, size_t size)
+{
+ return (strlcat(dst, src, size) >= size);
+}
+
/* Rather Obvious */
char *
copy_string(char *str)
[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.5 (FreeBSD)
Comment: For info see http://www.gnupg.org
iD8DBQE6+QAXWry0BWjoQKURAn6pAKDcFxG90FconKIQEqJZmOZMZxzadACgj+x4
s3cKXMjwrNLuZ+BXkgLAJ4U=
=V5Og
-----END PGP SIGNATURE-----
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010509013016.A29331>
