various cleanups including:
+ check a possible buffer overflow when creating a temp file, submitted by Christoph Mallon + remove stale struct definitions + clarify the use of dflag and remove useless checks MFC after: 3 days
This commit is contained in:
parent
14560a3610
commit
9cb138bb35
@ -57,37 +57,19 @@
|
|||||||
#define MAXRECSIZE 1024
|
#define MAXRECSIZE 1024
|
||||||
#define check(val) if ((error = (val)) != 0) break
|
#define check(val) if ((error = (val)) != 0) break
|
||||||
|
|
||||||
#ifndef min
|
static int dflag; /* do not create a hint file, only write on stdout */
|
||||||
#define min(a,b) (((a)<(b)) ? (a) : (b))
|
static int verbose;
|
||||||
#endif
|
|
||||||
|
|
||||||
struct mod_info {
|
static FILE *fxref; /* current hints file */
|
||||||
char* mi_name;
|
|
||||||
int mi_ver;
|
|
||||||
SLIST_ENTRY(mod_info) mi_next;
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef notnow
|
|
||||||
struct kld_info {
|
|
||||||
char* k_filename;
|
|
||||||
SLIST_HEAD(mod_list_head, mod_info) k_modules;
|
|
||||||
SLIST_ENTRY(kld_info) k_next;
|
|
||||||
};
|
|
||||||
|
|
||||||
SLIST_HEAD(kld_list_head, kld_info) kldlist;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int dflag, verbose;
|
|
||||||
|
|
||||||
FILE *fxref;
|
|
||||||
|
|
||||||
static const char *xref_file = "linker.hints";
|
static const char *xref_file = "linker.hints";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A record is stored in the static buffer recbuf before going to disk.
|
||||||
|
*/
|
||||||
static char recbuf[MAXRECSIZE];
|
static char recbuf[MAXRECSIZE];
|
||||||
static int recpos, reccnt;
|
static int recpos; /* current write position */
|
||||||
|
static int reccnt; /* total record written to this file so far */
|
||||||
FILE *maketempfile(char *, const char *);
|
|
||||||
static void usage(void);
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
intalign(void)
|
intalign(void)
|
||||||
@ -105,7 +87,7 @@ record_start(void)
|
|||||||
static int
|
static int
|
||||||
record_end(void)
|
record_end(void)
|
||||||
{
|
{
|
||||||
if (dflag || recpos == 0)
|
if (recpos == 0)
|
||||||
return 0;
|
return 0;
|
||||||
reccnt++;
|
reccnt++;
|
||||||
intalign();
|
intalign();
|
||||||
@ -123,6 +105,9 @@ record_buf(const void *buf, int size)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* An int is stored in host order and aligned
|
||||||
|
*/
|
||||||
static int
|
static int
|
||||||
record_int(int val)
|
record_int(int val)
|
||||||
{
|
{
|
||||||
@ -130,21 +115,21 @@ record_int(int val)
|
|||||||
return record_buf(&val, sizeof(val));
|
return record_buf(&val, sizeof(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
/*
|
||||||
record_byte(u_char val)
|
* A string is stored as 1-byte length plus data, no padding
|
||||||
{
|
*/
|
||||||
return record_buf(&val, sizeof(val));
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
record_string(const char *str)
|
record_string(const char *str)
|
||||||
{
|
{
|
||||||
int len = strlen(str);
|
int len, error;
|
||||||
int error;
|
u_char val;
|
||||||
|
|
||||||
if (dflag)
|
if (dflag)
|
||||||
return 0;
|
return 0;
|
||||||
error = record_byte(len);
|
val = len = strlen(str);
|
||||||
|
if (len > 255)
|
||||||
|
errx(1, "string %s too long", str);
|
||||||
|
error = record_buf(&val, sizeof(val));
|
||||||
if (error)
|
if (error)
|
||||||
return error;
|
return error;
|
||||||
return record_buf(str, len);
|
return record_buf(str, len);
|
||||||
@ -170,21 +155,23 @@ parse_entry(struct mod_metadata *md, const char *cval,
|
|||||||
break;
|
break;
|
||||||
case MDT_VERSION:
|
case MDT_VERSION:
|
||||||
check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv));
|
check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv));
|
||||||
|
if (dflag) {
|
||||||
|
printf(" interface %s.%d\n", cval, mdv.mv_version);
|
||||||
|
} else {
|
||||||
record_int(MDT_VERSION);
|
record_int(MDT_VERSION);
|
||||||
record_string(cval);
|
record_string(cval);
|
||||||
record_int(mdv.mv_version);
|
record_int(mdv.mv_version);
|
||||||
record_string(kldname);
|
record_string(kldname);
|
||||||
if (!dflag)
|
}
|
||||||
break;
|
|
||||||
printf(" interface %s.%d\n", cval, mdv.mv_version);
|
|
||||||
break;
|
break;
|
||||||
case MDT_MODULE:
|
case MDT_MODULE:
|
||||||
|
if (dflag) {
|
||||||
|
printf(" module %s\n", cval);
|
||||||
|
} else {
|
||||||
record_int(MDT_MODULE);
|
record_int(MDT_MODULE);
|
||||||
record_string(cval);
|
record_string(cval);
|
||||||
record_string(kldname);
|
record_string(kldname);
|
||||||
if (!dflag)
|
}
|
||||||
break;
|
|
||||||
printf(" module %s\n", cval);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
warnx("unknown metadata record %d in file %s", md->md_type, kldname);
|
warnx("unknown metadata record %d in file %s", md->md_type, kldname);
|
||||||
@ -199,8 +186,6 @@ read_kld(char *filename, char *kldname)
|
|||||||
{
|
{
|
||||||
struct mod_metadata md;
|
struct mod_metadata md;
|
||||||
struct elf_file ef;
|
struct elf_file ef;
|
||||||
/* struct kld_info *kip;
|
|
||||||
struct mod_info *mip;*/
|
|
||||||
void **p, **orgp;
|
void **p, **orgp;
|
||||||
int error, eftype, nmlen;
|
int error, eftype, nmlen;
|
||||||
long start, finish, entries;
|
long start, finish, entries;
|
||||||
@ -224,8 +209,9 @@ read_kld(char *filename, char *kldname)
|
|||||||
}
|
}
|
||||||
if (!dflag) {
|
if (!dflag) {
|
||||||
cp = strrchr(kldname, '.');
|
cp = strrchr(kldname, '.');
|
||||||
nmlen = cp ? min(MAXMODNAME, cp - kldname) :
|
nmlen = (cp != NULL) ? cp - kldname : (int)strlen(kldname);
|
||||||
min(MAXMODNAME, strlen(kldname));
|
if (nmlen > MAXMODNAME)
|
||||||
|
nmlen = MAXMODNAME;
|
||||||
strlcpy(kldmodname, kldname, nmlen);
|
strlcpy(kldmodname, kldname, nmlen);
|
||||||
/* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/
|
/* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/
|
||||||
}
|
}
|
||||||
@ -252,27 +238,43 @@ read_kld(char *filename, char *kldname)
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *
|
/*
|
||||||
|
* Create a temp file in directory root, make sure we don't
|
||||||
|
* overflow the buffer for the destination name
|
||||||
|
*/
|
||||||
|
static FILE *
|
||||||
maketempfile(char *dest, const char *root)
|
maketempfile(char *dest, const char *root)
|
||||||
{
|
{
|
||||||
char *p;
|
char *p;
|
||||||
int fd;
|
int n, fd;
|
||||||
|
|
||||||
strlcpy(dest, root, MAXPATHLEN);
|
p = strrchr(root, '/');
|
||||||
|
n = p != NULL ? p - root + 1 : 0;
|
||||||
|
if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >=
|
||||||
|
MAXPATHLEN) {
|
||||||
|
errno = ENAMETOOLONG;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if ((p = strrchr(dest, '/')) != 0)
|
|
||||||
p++;
|
|
||||||
else
|
|
||||||
p = dest;
|
|
||||||
strcpy(p, "lhint.XXXXXX");
|
|
||||||
fd = mkstemp(dest);
|
fd = mkstemp(dest);
|
||||||
if (fd >= 0)
|
if (fd < 0)
|
||||||
|
return NULL;
|
||||||
fchmod(fd, 0644); /* nothing secret in the file */
|
fchmod(fd, 0644); /* nothing secret in the file */
|
||||||
return ((fd == -1) ? NULL : fdopen(fd, "w+"));
|
return fdopen(fd, "w+");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
|
static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
fprintf(stderr, "%s\n",
|
||||||
|
"usage: kldxref [-Rdv] [-f hintsfile] path ..."
|
||||||
|
);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -282,20 +284,19 @@ main(int argc, char *argv[])
|
|||||||
struct stat sb;
|
struct stat sb;
|
||||||
|
|
||||||
fts_options = FTS_PHYSICAL;
|
fts_options = FTS_PHYSICAL;
|
||||||
/* SLIST_INIT(&kldlist);*/
|
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
|
while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'd':
|
case 'd': /* no hint file, only print on stdout */
|
||||||
dflag = 1;
|
dflag = 1;
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f': /* use this name instead of linker.hints */
|
||||||
xref_file = optarg;
|
xref_file = optarg;
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
verbose++;
|
verbose++;
|
||||||
break;
|
break;
|
||||||
case 'R':
|
case 'R': /* recurse on directories */
|
||||||
fts_options |= FTS_COMFOLLOW;
|
fts_options |= FTS_COMFOLLOW;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -321,19 +322,22 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
p = fts_read(ftsp);
|
p = fts_read(ftsp);
|
||||||
if ((p == NULL || p->fts_info == FTS_D) && !dflag && fxref) {
|
if ((p == NULL || p->fts_info == FTS_D) && fxref) {
|
||||||
|
/* close and rename the current hint file */
|
||||||
fclose(fxref);
|
fclose(fxref);
|
||||||
fxref = NULL;
|
fxref = NULL;
|
||||||
if (reccnt) {
|
if (reccnt) {
|
||||||
rename(tempname, xrefname);
|
rename(tempname, xrefname);
|
||||||
} else {
|
} else {
|
||||||
|
/* didn't find any entry, ignore this file */
|
||||||
unlink(tempname);
|
unlink(tempname);
|
||||||
unlink(xrefname);
|
unlink(xrefname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
break;
|
break;
|
||||||
if (p && p->fts_info == FTS_D && !dflag) {
|
if (p->fts_info == FTS_D && !dflag) {
|
||||||
|
/* visiting a new directory, create a new hint file */
|
||||||
snprintf(xrefname, sizeof(xrefname), "%s/%s",
|
snprintf(xrefname, sizeof(xrefname), "%s/%s",
|
||||||
ftsp->fts_path, xref_file);
|
ftsp->fts_path, xref_file);
|
||||||
fxref = maketempfile(tempname, ftsp->fts_path);
|
fxref = maketempfile(tempname, ftsp->fts_path);
|
||||||
@ -343,6 +347,7 @@ main(int argc, char *argv[])
|
|||||||
fwrite(&ival, sizeof(ival), 1, fxref);
|
fwrite(&ival, sizeof(ival), 1, fxref);
|
||||||
reccnt = 0;
|
reccnt = 0;
|
||||||
}
|
}
|
||||||
|
/* skip non-files or .symbols entries */
|
||||||
if (p->fts_info != FTS_F)
|
if (p->fts_info != FTS_F)
|
||||||
continue;
|
continue;
|
||||||
if (p->fts_namelen >= 8 &&
|
if (p->fts_namelen >= 8 &&
|
||||||
@ -353,13 +358,3 @@ main(int argc, char *argv[])
|
|||||||
fts_close(ftsp);
|
fts_close(ftsp);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
usage(void)
|
|
||||||
{
|
|
||||||
|
|
||||||
fprintf(stderr, "%s\n",
|
|
||||||
"usage: kldxref [-Rdv] [-f hintsfile] path ..."
|
|
||||||
);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user