Fix all (hopefully) occurencies of strcat and strncat which may cause buffer overflows

This commit is contained in:
Silvan Calarco 2015-06-17 20:58:26 +02:00
parent 538a1d7696
commit 8e9c1edb36
6 changed files with 77 additions and 77 deletions

View File

@ -42,15 +42,15 @@
int
generateBuildInfo(struct configTag *configtag, int arch)
{
char foutname[1024], fsourcesname[1024], fdir[1024];
char foutname[PATH_MAX], fsourcesname[PATH_MAX], fdir[PATH_MAX];
FILE *fout, *fsources;
int i, j;
struct headerSourceList *currheadersourcelist;
struct headerList *currchild;
strncpy(fdir, configtag->repository_source_dir, 1024);
strncpy(fsourcesname, fdir, 1024);
strncat(fsourcesname, "buildinfo/sources.dat", 1024);
snprintf(fdir, sizeof(fdir), "%s", configtag->repository_source_dir);
strncpy(fsourcesname, fdir, sizeof(fsourcesname));
strncat(fsourcesname, "buildinfo/sources.dat", sizeof(fsourcesname) - strlen(fsourcesname));
if ((fsources = fopen(fsourcesname, "w")) == NULL) {
perror(fsourcesname);
@ -59,10 +59,10 @@ generateBuildInfo(struct configTag *configtag, int arch)
currheadersourcelist = configtag->headersourcelist;
while (currheadersourcelist) {
strncpy(foutname, fdir, 1024);
strncat(foutname, "buildinfo/", 1024);
strncat(foutname, currheadersourcelist->name, 1024);
strncat(foutname, ".info", 1024);
strncpy(foutname, fdir, sizeof(foutname));
strncat(foutname, "buildinfo/", sizeof(foutname) - strlen(foutname));
strncat(foutname, currheadersourcelist->name, sizeof(foutname) - strlen(foutname));
strncat(foutname, ".info", sizeof(foutname) - strlen(foutname));
if ((fout = fopen(foutname, "w")) == NULL) {
perror(foutname);

View File

@ -937,7 +937,7 @@ main(int argc, char *argv[])
int i = 0,hasbuilds[ARCHS_MAX],ptharg[ARCHS_MAX];
pthread_t pth[ARCHS_MAX];
char warning[PATH_MAX];
char warning[8096];
time_t start_time, stop_time;
@ -1139,7 +1139,7 @@ main(int argc, char *argv[])
warning[0]=0;
for (i = 0; i < ARCHS_MAX && configtag->arch[i]; i++) {
snprintf(&warning[strlen(warning)],PATH_MAX-strlen(warning)," %s", configtag->arch[i]);
snprintf(&warning[strlen(warning)],sizeof(warning)-strlen(warning)," %s", configtag->arch[i]);
}
if (!quietmode)
fprintf(stdout, "Scanning binary packages for archs:%s...\n",warning);
@ -1174,8 +1174,8 @@ main(int argc, char *argv[])
warning[0] = '\0';
for (i = 0; i < ARCHS_MAX && configtag->arch[i]; i++) {
if (hasbuilds[i] == 1) {
strncat(warning," ",PATH_MAX);
strncat(warning,configtag->arch[i],PATH_MAX);
strncat(warning, " ", sizeof(warning) - strlen(warning));
strncat(warning, configtag->arch[i], sizeof(warning) - strlen(warning));
}
}
if (warning[0] == '\0') {
@ -1191,9 +1191,9 @@ main(int argc, char *argv[])
if ((hasbuilds[i] == -1) &&
(currheadersourcelist->altrepository == configtag->repository_level)) {
if (warning[0] == '\0')
strncat(warning, "requires port to arch(s):", PATH_MAX);
strncat(warning," ",PATH_MAX);
strncat(warning,configtag->arch[i],PATH_MAX);
strncat(warning, "requires port to arch(s):", sizeof(warning) - strlen(warning));
strncat(warning," ", sizeof(warning) - strlen(warning));
strncat(warning,configtag->arch[i], sizeof(warning) - strlen(warning));
}
}
if (warning[0] != '\0') {
@ -1213,13 +1213,13 @@ main(int argc, char *argv[])
}
if (currrebuild) {
strncat(warning,"need to be rebuilt:", PATH_MAX);
strncat(warning,"need to be rebuilt:", sizeof(warning) - strlen(warning));
while (currrebuild) {
strncat(warning," ",PATH_MAX);
strncat(warning,currrebuild->sourceheader->name,PATH_MAX);
strncat(warning,"(",PATH_MAX);
strncat(warning,currrebuild->provider->arch,PATH_MAX);
strncat(warning,")",PATH_MAX);
strncat(warning," ", sizeof(warning) - strlen(warning));
strncat(warning,currrebuild->sourceheader->name, sizeof(warning) - strlen(warning));
strncat(warning,"(", sizeof(warning) - strlen(warning));
strncat(warning,currrebuild->provider->arch, sizeof(warning) - strlen(warning));
strncat(warning,")", sizeof(warning) - strlen(warning));
currrebuild = currrebuild->next;
}
}

View File

@ -409,7 +409,7 @@ int findFileIdFromPath(sqlite3 *db, char** path) {
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK && sqlite3_step(stmt1) == SQLITE_ROW) {
parent = sqlite3_column_int(stmt1,0);
sqlite3_finalize(stmt1);
strcat(linkpath, "/");
strncat(linkpath, "/", strlen(*path) - strlen(linkpath));
strncat(linkpath, buffer, strlen(*path) - strlen(linkpath));
printf("/<a href='javascript:distroquery_request(\"repository=%s&arch=%s&path=%s\")'>%s</a>",
query_repository, query_arch, linkpath, buffer);
@ -1558,10 +1558,10 @@ void parse_request_variables(char *data) {
search_files = strstr(valuetok, "false") != valuetok;
}
if (vartok && valuetok) {
strcat(query_next, vartok);
strcat(query_next, "=");
strcat(query_next, valuetok);
strcat(query_next, "&");
strncat(query_next, vartok, sizeof(query_next) - strlen(query_next));
strncat(query_next, "=", sizeof(query_next) - strlen(query_next));
strncat(query_next, valuetok, sizeof(query_next) - strlen(query_next));
strncat(query_next, "&", sizeof(query_next) - strlen(query_next));
}
}
}

View File

@ -172,8 +172,8 @@ struct configTag* read_configuration(const char *confFile)
if (configdefaults.html_basedir) {
strncpy(buf, configdefaults.html_basedir, PATH_MAX);
strncat(buf, newconfigtag->tag, PATH_MAX);
strncat(buf, "/", PATH_MAX);
strncat(buf, newconfigtag->tag, sizeof(buf) - strlen(buf));
strncat(buf, "/", sizeof(buf) - strlen(buf));
newconfigtag->html_dir = (char *) strdup(buf);
}
@ -326,7 +326,7 @@ struct configTag* read_configuration(const char *confFile)
if (!currconfigtag->repository_source_dir) {
strncpy(buf, currconfigtag->repository_dir, PATH_MAX);
strncat(buf, "/SRPMS.base/", PATH_MAX);
strncat(buf, "/SRPMS.base/", sizeof(buf) - strlen(buf));
currconfigtag->repository_source_dir =
(char *) strdup(buf);
}
@ -402,11 +402,11 @@ void logmsg(int level, const char *msg, ...) {
va_list ap;
static char oldmsg[20][256];
static int curroldmsg = 0;
char newmsg[1024];
char newmsg[PATH_MAX];
int i;
va_start(ap, msg);
vsnprintf((char*)&newmsg, 1024, msg, ap);
vsnprintf((char*)&newmsg, PATH_MAX, msg, ap);
va_end(ap);
for (i = 0; i < 20; i++) {

View File

@ -65,7 +65,7 @@
int rpmselector(const struct dirent *entry);
int sourcerpmselector(const struct dirent *entry);
const int bufsize = 1024;
const int bufsize = PATH_MAX;
struct warningList* addWarning(struct headerSourceList *pkg, char* text)
{
@ -534,7 +534,7 @@ findOrCreateFileTreeEntry(struct fileTree* *first,char* findname, int arch)
{
struct fileTree *currdir,*prevdir=NULL;
char *pstart,*pend;
char f[1024];
char f[PATH_MAX];
int l;
currdir = *first;

View File

@ -84,7 +84,7 @@ int
printHTMLWarnings(FILE *fout, struct configTag *configtag, struct headerSourceList* pkg, int mode) {
char warningsfile[PATH_MAX];
char buf[1024];
char buf[PATH_MAX];
struct stat s;
FILE *fin;
int n;
@ -122,7 +122,7 @@ printHTMLWarnings(FILE *fout, struct configTag *configtag, struct headerSourceLi
fprintf(fout,"&nbsp;&bull;&nbsp;Comment:");
while (!feof(fin)) {
n = fread(buf,1,1024,fin);
n = fread(buf, 1, sizeof(buf), fin);
fwrite(buf,1,n,fout);
}
if (mode == 0) {
@ -223,7 +223,7 @@ void printpkgicon(FILE *fout, struct configTag *configtag, struct headerSourceLi
int
generateMaintainersPages(struct configTag *configtag)
{
char idxfile[1024],outfile[1024],unmaintfile[1024];
char idxfile[PATH_MAX],outfile[PATH_MAX],unmaintfile[PATH_MAX];
FILE *idx=NULL,*out=NULL,*unmaint=NULL;
int i,pkgnum,unmaintpkgnum;
struct stat buf;
@ -234,7 +234,7 @@ generateMaintainersPages(struct configTag *configtag)
configtag->stats.headersourcecount, sizeof(struct headerSourceList *),
comparePackagers);
snprintf(outfile,1024,"%smaintainers",configtag->html_dir);
snprintf(outfile, PATH_MAX, "%smaintainers", configtag->html_dir);
if (stat(outfile,&buf)) {
if (mkdir(outfile,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
logmsg(LOG_ERROR,"cannot create %s directory; aborting.",outfile);
@ -247,7 +247,7 @@ generateMaintainersPages(struct configTag *configtag)
}
}
snprintf(idxfile,1024,"%s_maintainers.inc",configtag->html_dir);
snprintf(idxfile, PATH_MAX, "%s_maintainers.inc", configtag->html_dir);
if ((idx = fopen(idxfile, "w")) == NULL) {
perror(idxfile);
@ -259,7 +259,7 @@ generateMaintainersPages(struct configTag *configtag)
configtag->tag,
configtag->description);
snprintf(unmaintfile,1024,"%smaintainers/unmaintained.inc",configtag->html_dir);
snprintf(unmaintfile, PATH_MAX, "%smaintainers/unmaintained.inc", configtag->html_dir);
if ((unmaint = fopen(unmaintfile, "w")) == NULL) {
perror(unmaintfile);
@ -283,7 +283,7 @@ generateMaintainersPages(struct configTag *configtag)
if ((configtag->stats.headersourcelistvec[i])->packager->role &
PACKAGER_ROLE_MAINTAINER) {
snprintf(outfile,1024,"%smaintainers/%s.inc",
snprintf(outfile, PATH_MAX, "%smaintainers/%s.inc",
configtag->html_dir,
(configtag->stats.headersourcelistvec[i])->packager->name);
@ -380,8 +380,8 @@ generateMaintainersPages(struct configTag *configtag)
int
generateStats(struct configTag *configtag,int arch)
{
char outfile[1024];
char rssfile[1024];
char outfile[PATH_MAX];
char rssfile[PATH_MAX];
char buffer[PATH_MAX];
FILE *htmlout=NULL,*htmloldout,*rssout,*groupout;
struct stat buf;
@ -410,7 +410,7 @@ generateStats(struct configTag *configtag,int arch)
configtag->stats.headersourcecount, sizeof(struct headerSourceList *),
compareGroup);
snprintf(outfile,1024,"%s_groups.inc",configtag->html_dir);
snprintf(outfile, PATH_MAX, "%s_groups.inc", configtag->html_dir);
if ((groupout = fopen(outfile, "w")) == NULL) {
perror(outfile);
@ -457,7 +457,7 @@ generateStats(struct configTag *configtag,int arch)
exit(1);
}
}
strncat(outfile, "/_index.inc", 1024);
strncat(outfile, "/_index.inc", sizeof(outfile) - strlen(outfile));
if ((htmlout = fopen(outfile, "w")) == NULL) {
perror(outfile);
return 1;
@ -500,8 +500,8 @@ generateStats(struct configTag *configtag,int arch)
fclose(groupout);
}
strncpy(outfile, configtag->html_dir, 1024);
strncat(outfile, "_recent.inc", 1024);
strncpy(outfile, configtag->html_dir, PATH_MAX);
strncat(outfile, "_recent.inc", sizeof(outfile) - strlen(outfile));
if ((htmlout = fopen(outfile, "w")) == NULL) {
perror(outfile);
return 1;
@ -516,8 +516,8 @@ generateStats(struct configTag *configtag,int arch)
fprintf(htmlout, "Recent builds:<br>\n");
}*/
strncpy(outfile, configtag->html_dir, 1024);
strncat(outfile, "_oldest.inc", 1024);
strncpy(outfile, configtag->html_dir, PATH_MAX);
strncat(outfile, "_oldest.inc", sizeof(outfile) - strlen(outfile));
if ((htmloldout = fopen(outfile, "w")) == NULL) {
perror(outfile);
return 1;
@ -527,8 +527,8 @@ generateStats(struct configTag *configtag,int arch)
fprintf(htmloldout, "Oldest builds:<br>\n");
}*/
strncpy(rssfile, configtag->html_dir, 1024);
strncat(rssfile, "recent.rss", 1024);
strncpy(rssfile, configtag->html_dir, PATH_MAX);
strncat(rssfile, "recent.rss", sizeof(rssfile) - strlen(rssfile));
if ((rssout = fopen(rssfile, "w")) == NULL) {
perror(rssfile);
return 1;
@ -643,7 +643,7 @@ generateStats(struct configTag *configtag,int arch)
fclose(rssout);
/* create APT repository file */
snprintf(outfile,1024,"%s%s-%s.list",
snprintf(outfile, PATH_MAX, "%s%s-%s.list",
configtag->html_dir,
configtag->configdefaults->distribution_name,
configtag->tag);
@ -683,7 +683,7 @@ generateStats(struct configTag *configtag,int arch)
}
/* create Smart Package Manager channel file */
snprintf(outfile,1024,"%s%s-%s.smart",
snprintf(outfile, PATH_MAX, "%s%s-%s.smart",
configtag->html_dir,
configtag->configdefaults->distribution_name,
configtag->tag);
@ -727,7 +727,7 @@ generateStats(struct configTag *configtag,int arch)
tmdate.tm_mday = 1;
tmdate.tm_mon = 0;
snprintf(outfile,1024,"%s_changelog.inc",configtag->html_dir);
snprintf(outfile, PATH_MAX, "%s_changelog.inc", configtag->html_dir);
if ((htmlout = fopen(outfile, "w")) == NULL) {
perror(outfile);
@ -757,7 +757,7 @@ generateHTMLMainIndex(struct configTag *configtag)
}
strncpy(indexfile, configtag->configdefaults->html_basedir, PATH_MAX);
strncat(indexfile, "_index.inc", PATH_MAX);
strncat(indexfile, "_index.inc", sizeof(indexfile) - strlen(indexfile));
if ((fout = fopen(indexfile, "w")) == NULL) {
perror(indexfile);
@ -846,7 +846,7 @@ generateHTML_SRPMSFiles(struct configTag *configtag)
char indexfile[PATH_MAX];
char htmlfile[PATH_MAX];
char warningsdir[PATH_MAX];
char buffer[PATH_MAX],buffer2[1024];
char buffer[PATH_MAX],buffer2[PATH_MAX];
int c,i,j,arch,idx;
char *st;
char curr_letter,curr_anchor ='a'-1;
@ -955,7 +955,7 @@ generateHTML_SRPMSFiles(struct configTag *configtag)
return 1;
}
get_favicon_from_url(currheadersourcelist->url,buffer2,1024);
get_favicon_from_url(currheadersourcelist->url, buffer2, PATH_MAX);
fprintf(fout,
"<h1><img src=\"%s\" width=\"16\" height=\"16\">"
@ -989,7 +989,7 @@ generateHTML_SRPMSFiles(struct configTag *configtag)
"<tr><td>Group:</td><td><a href=\"%stag=%s&amp;group=%s\">%s</a></td></tr>\n",
configtag->configdefaults->url_prefix,
configtag->tag,
groupdirname(currheadersourcelist->group,buffer2,1024),
groupdirname(currheadersourcelist->group, buffer2, PATH_MAX),
htmlclean(currheadersourcelist->group,buffer,PATH_MAX));
fprintf(fout,
@ -1426,7 +1426,7 @@ generateHTMLFiles(struct configTag *ct, int arch)
perror(htmlfile);
return 1;
}
get_favicon_from_url(currheaderlist->sourceheader->url,buffer,1024);
get_favicon_from_url(currheaderlist->sourceheader->url, buffer, PATH_MAX);
fprintf(fout,
"<h1><img src=\"%s\" width=\"16\" height=\"16\">"
@ -1751,8 +1751,8 @@ void print_contents_subtree(FILE *f,
(ft->provider[k]->altrepository == ct->repository_level))) {
ftname(ft,buf,bufsize);
if ((j=strlen(buf)) < 60) {
for (i=(60-j)/8; i>0; i--) strncat(buf,"\t",1024);
while (strlen(buf) < 60) strncat(buf," ",1024);
for (i=(60-j)/8; i>0; i--) strncat(buf, "\t", bufsize - strlen(buf));
while (strlen(buf) < 60) strncat(buf, " ", bufsize - strlen(buf));
}
fprintf(f, "%s %s/%s\n",buf,ct->tag,ft->provider[0]->name);
}
@ -1768,23 +1768,23 @@ int
print_datatables(struct configTag *ct, int arch) {
FILE *fbd,*fd,*fv,*fb,*fbsh,*fs,*fc,*fw;
char builddeps_filename[1024], deps_filename[1024], virtual_filename[1024],
builds_filename[1024], builds_sh_filename[1024], sources_filename[1024],
contents_filename[1024], warnings_filename[1024], buf[1024];
char builddeps_filename[PATH_MAX], deps_filename[PATH_MAX], virtual_filename[PATH_MAX],
builds_filename[PATH_MAX], builds_sh_filename[PATH_MAX], sources_filename[PATH_MAX],
contents_filename[PATH_MAX], warnings_filename[PATH_MAX], buf[PATH_MAX];
char obsoletebuf[PATH_MAX];
struct headerList *currheaderlist, *currchild;
struct headerSourceList *currheadersourcelist, *oldheadersourcelist;
struct rebuildList *currrebuild;
int i, nonobsoletednumproviders;
snprintf(builddeps_filename,1024,"%sbuilddeps-%s",ct->html_dir,ct->arch[arch]);
snprintf(deps_filename,1024,"%sdeps-%s",ct->html_dir,ct->arch[arch]);
snprintf(virtual_filename,1024,"%svirtual-%s",ct->html_dir,ct->arch[arch]);
snprintf(builds_filename,1024,"%sbuilds-%s",ct->html_dir,ct->arch[arch]);
snprintf(builds_sh_filename,1024,"%sbuilds-%s.sh",ct->html_dir,ct->arch[arch]);
snprintf(sources_filename,1024,"%ssources-%s",ct->html_dir,ct->arch[arch]);
snprintf(contents_filename,1024,"%scontentslist-%s",ct->repository_dir,ct->arch[arch]);
snprintf(warnings_filename,1024,"%swarnings-%s",ct->repository_dir,ct->arch[arch]);
snprintf(builddeps_filename, PATH_MAX, "%sbuilddeps-%s", ct->html_dir, ct->arch[arch]);
snprintf(deps_filename, PATH_MAX, "%sdeps-%s", ct->html_dir, ct->arch[arch]);
snprintf(virtual_filename, PATH_MAX, "%svirtual-%s", ct->html_dir, ct->arch[arch]);
snprintf(builds_filename, PATH_MAX, "%sbuilds-%s", ct->html_dir, ct->arch[arch]);
snprintf(builds_sh_filename, PATH_MAX, "%sbuilds-%s.sh", ct->html_dir, ct->arch[arch]);
snprintf(sources_filename, PATH_MAX, "%ssources-%s", ct->html_dir, ct->arch[arch]);
snprintf(contents_filename, PATH_MAX, "%scontentslist-%s", ct->repository_dir, ct->arch[arch]);
snprintf(warnings_filename, PATH_MAX, "%swarnings-%s", ct->repository_dir, ct->arch[arch]);
fbd=fopen(builddeps_filename,"w");
if (!fbd) {
@ -1935,7 +1935,7 @@ print_datatables(struct configTag *ct, int arch) {
//write contents (filenames)
logmsg(LOG_DEBUG,"writing contents file");
print_contents_subtree(fc,ct->filetree[arch],ct,buf,1024);
print_contents_subtree(fc, ct->filetree[arch], ct, buf, PATH_MAX);
logmsg(LOG_DEBUG,"writing deps files (2)");
currheaderlist = ct->headerlist[arch];
@ -2045,10 +2045,10 @@ print_datatables(struct configTag *ct, int arch) {
if (currchild) {
if (!strncmp(currheadersourcelist->arch,"noarch",1024))
snprintf(obsoletebuf,1024,"noarch");
if (!strncmp(currheadersourcelist->arch, "noarch", PATH_MAX))
snprintf(obsoletebuf, PATH_MAX, "noarch");
else
snprintf(obsoletebuf,1024,"%s",ct->arch[arch]);
snprintf(obsoletebuf, PATH_MAX, "%s", ct->arch[arch]);
fprintf(fbsh,"[ \"$pkg\" = \"%s\" ] && { pkg_header=(%s %s %s %s \"%s\" \"%s\" %ld %ld %d %s); ",
currheadersourcelist->name,
currheadersourcelist->name,
@ -2080,8 +2080,8 @@ print_datatables(struct configTag *ct, int arch) {
currheadersourcelist->name, PATH_MAX);
break;
}
if (obsoletebuf[0] != '\0') strcat(obsoletebuf," ");
strcat(obsoletebuf,currchild->obsoletename[i]);
if (obsoletebuf[0] != '\0') strncat(obsoletebuf, " ", sizeof(obsoletebuf) - strlen(obsoletebuf));
strncat(obsoletebuf, currchild->obsoletename[i], sizeof(obsoletebuf) - strlen(obsoletebuf));
}
currchild = currchild->nextbrother;
if (currchild) fprintf(fbsh," ");