Skip to content

Commit 94a2bcc

Browse files
npe9Noah Evans
authored andcommitted
Add access() calls to ensure fopen(...,"r") doesn't segfault
One of the invariants of the fopen() function called with the "r" option is that the file exist before the function is called. If this is not the case behavior is undefined (in the case of glibc on some Crays and Redhat machines this means you get a segfault in fileno_unlocked()). This commit is a first attempt at ensuring this invariant holds when calls to fopen() are made.
1 parent fba6990 commit 94a2bcc

File tree

40 files changed

+190
-65
lines changed

40 files changed

+190
-65
lines changed

ompi/debuggers/dlopen_test.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ static int do_test(void)
6565

6666
/* Double check that the .la file is there that we expect; if it's
6767
not, skip this test. */
68+
if (!access(full_filename, F_OK)) {
69+
fprintf(stderr,
70+
"File %s.la doesn't seem to exist; skipping this test\n",
71+
full_filename);
72+
exit(77);
73+
}
6874
fp = fopen(full_filename, "r");
69-
if (NULL == fp) {
70-
fprintf(stderr,
71-
"File %s.la doesn't seem to exist; skipping this test\n",
72-
full_filename);
73-
exit(77);
75+
if (fp == NULL) {
76+
fprintf(stderr, "File %s.la fopen failed; skipping this test\n",
77+
full_filename);
78+
exit(77);
7479
}
7580
/* We know the .la file is there, so read it, looking for the
7681
dlopen value. If the dlopen value is '' (i.e., empty), then

ompi/debuggers/ompi_debuggers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static void check(char *dir, char *file, char **locations)
148148
FILE *fp;
149149

150150
/* Just try to open the file */
151-
if (NULL != (fp = fopen(str, "r"))) {
151+
if (access(str, F_OK) && (NULL != (fp = fopen(str, "r"))) {
152152
fclose(fp);
153153
opal_argv_append_nosize(&locations, file);
154154
}

ompi/mca/coll/tuned/coll_tuned_dynamic_file.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ int ompi_coll_tuned_read_rules_config_file (char *fname, ompi_coll_alg_rule_t**
8888
return (-3);
8989
}
9090

91+
if (!access(fname, F_OK)) {
92+
OPAL_OUTPUT((ompi_coll_tuned_stream,"rules file does not exist [%s]\n", fname));
93+
goto on_file_error;
94+
}
9195
fptr = fopen (fname, "r");
9296
if (!fptr) {
9397
OPAL_OUTPUT((ompi_coll_tuned_stream,"cannot read rules file [%s]\n", fname));

ompi/mca/topo/treematch/treematch/tm_mapping.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ int nb_lines(char *filename)
7373
char line[LINE_SIZE];
7474
int N = 0;
7575

76+
if(!access(filename, F_OK)) {
77+
if(tm_get_verbose_level() >= CRITICAL)
78+
fprintf(stderr,"%s does not exist\n",filename);
79+
exit(-1);
80+
}
81+
7682
if(!(pf = fopen(filename,"r"))){
7783
if(tm_get_verbose_level() >= CRITICAL)
7884
fprintf(stderr,"Cannot open %s\n",filename);
@@ -97,6 +103,11 @@ void init_mat(char *filename,int N, double **mat, double *sum_row)
97103
int i,j;
98104
unsigned int vl = tm_get_verbose_level();
99105

106+
if(!access(filename, F_OK)) {
107+
if(tm_get_verbose_level() >= CRITICAL)
108+
fprintf(stderr,"%s does not exist\n",filename);
109+
exit(-1);
110+
}
100111

101112
if(!(pf=fopen(filename,"r"))){
102113
if(vl >= CRITICAL)

ompi/mca/topo/treematch/treematch/tm_topology.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ int tm_topology_add_binding_constraints(char *constraints_filename, tm_topology
481481
int i,n;
482482
unsigned int vl = tm_get_verbose_level();
483483

484+
if (!access(constraints_filename, F_OK)) {
485+
if(tm_get_verbose_level() >= CRITICAL)
486+
fprintf(stderr,"%s does not exist\n",constraints_filename);
487+
exit(-1);
488+
}
484489

485490
if (!(pf = fopen(constraints_filename,"r"))) {
486491
if(vl >= CRITICAL)

opal/mca/btl/openib/btl_openib_ini.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,17 @@ static int parse_file(char *filename)
229229

230230
/* Open the file */
231231
ini_filename = filename;
232+
233+
if (!access(filename, F_OK)) {
234+
opal_show_help("help-mpi-btl-openib.txt", "ini file:file not found",
235+
true, filename);
236+
ret = OPAL_ERR_NOT_FOUND;
237+
goto cleanup;
238+
}
239+
232240
btl_openib_ini_yyin = fopen(filename, "r");
233241
if (NULL == btl_openib_ini_yyin) {
234-
opal_show_help("help-mpi-btl-openib.txt", "ini file:file not found",
242+
opal_show_help("help-mpi-btl-openib.txt", "ini file:could not open",
235243
true, filename);
236244
ret = OPAL_ERR_NOT_FOUND;
237245
goto cleanup;

opal/mca/btl/ugni/btl_ugni_init.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,13 @@ int mca_btl_ugni_init (void)
264264
mca_btl_ugni_component.virtual_device_count = MCA_BTL_UGNI_MAX_DEV_HANDLES;
265265
}
266266

267-
fh = fopen ("/proc/sys/kernel/pid_max", "r");
268-
if (NULL != fh) {
269-
fscanf (fh, "%d", &pid_max);
270-
fclose (fh);
267+
if (access("/proc/sys/kernel/pid_max", F_OK)) {
268+
fh = fopen ("/proc/sys/kernel/pid_max", "r");
269+
if (NULL != fh) {
270+
fscanf (fh, "%d", &pid_max);
271+
fclose (fh);
272+
}
271273
}
272-
273274
/* Use pid to generate the cdm_id. Although its not stated in the uGNI
274275
* documentation, the cdm_id only needs to be unique within a node for a
275276
* given ptag/cookie tuple */

opal/mca/crs/blcr/crs_blcr_module.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,14 @@ static int blcr_cold_start(opal_crs_blcr_snapshot_t *snapshot) {
739739
* Find the snapshot directory, read the metadata file
740740
*/
741741
if( NULL == snapshot->super.metadata ) {
742+
if(access(snapshot->super.metadata_filename, F_OK) {
743+
opal_output(mca_crs_blcr_component.super.output_handle,
744+
"crs:blcr: checkpoint(): Error: %s does not exist",
745+
snapshot->super.metadata_filename);
746+
exit_status = OPAL_ERROR;
747+
goto cleanup;
748+
}
749+
742750
if (NULL == (snapshot->super.metadata = fopen(snapshot->super.metadata_filename, "r")) ) {
743751
opal_output(mca_crs_blcr_component.super.output_handle,
744752
"crs:blcr: checkpoint(): Error: Unable to open the file (%s)",

opal/mca/crs/dmtcp/crs_dmtcp_module.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,14 @@ static int dmtcp_cold_start(opal_crs_dmtcp_snapshot_t *snapshot) {
477477
char **tmp_argv = NULL;
478478
char * component_name = NULL;
479479
int prev_pid;
480-
480+
char *smfname;
481481
/*
482482
* Find the snapshot directory, read the metadata file for
483483
* component name and previous pid
484484
*/
485485
if( NULL == snapshot->super.metadata ) {
486-
if (NULL == (snapshot->super.metadata = fopen(snapshot->super.metadata_filename, "r")) ) {
486+
smfname = snapshot->super.metadata_filename;
487+
if (!access(smfname, F_OK) || NULL == (snapshot->super.metadata = fopen(smfname, "r")) ) {
487488
opal_output(mca_crs_dmtcp_component.super.output_handle,
488489
"crs:dmtcp: dmtcp_cold_start(): Error: Unable to open the file (%s)",
489490
snapshot->super.metadata_filename);

opal/mca/if/linux_ipv6/if_linux_ipv6.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ static int if_linux_ipv6_open(void)
9595
{
9696
#if OPAL_ENABLE_IPV6
9797
FILE *f;
98-
if ((f = fopen("/proc/net/if_inet6", "r"))) {
98+
if (access("/proc/net/if_inet6", F_OK) &&
99+
(f = fopen("/proc/net/if_inet6", "r"))) {
99100
char ifname[IF_NAMESIZE];
100101
unsigned int idx, pfxlen, scope, dadstat;
101102
struct in6_addr a6;

0 commit comments

Comments
 (0)