Skip to content

Commit 94a2bcc

Browse files
npe9Noah Evans
authored and
Noah Evans
committed
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;

opal/mca/pmix/cray/pmix_cray_component.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,18 @@ static int pmix_cray_component_query(mca_base_module_t **module, int *priority)
114114

115115
/* disqualify ourselves if not running in a Cray PAGG container, or we
116116
were launched by the orte/mpirun launcher */
117-
fd = fopen(proc_job_file, "r");
118-
if ((fd == NULL) || (getenv("OMPI_NO_USE_CRAY_PMI") != NULL)) {
117+
if (!access(proc_job_file, F_OK) ||
118+
fd = fopen(proc_job_file, "r") == NULL ||
119+
(getenv("OMPI_NO_USE_CRAY_PMI") != NULL)) {
119120
*priority = 0;
120121
*module = NULL;
121122
rc = OPAL_ERROR;
122123
} else {
123124
snprintf(task_is_app_fname,sizeof(task_is_app_fname),
124125
"/proc/self/task/%ld/task_is_app",syscall(SYS_gettid));
125-
fd_task_is_app = fopen(task_is_app_fname, "r");
126-
if (fd_task_is_app != NULL) { /* okay we're in a PAGG container,
126+
if(access(fd_task_is_app, F_OK) &&
127+
NULL != fd_task_is_app = fopen(task_is_app_fname, "r") {
128+
/* okay we're in a PAGG container,
127129
and we are an app task (not just a process
128130
running on a mom node, for example),
129131
so we should give cray pmi a shot. */

opal/mca/pmix/pmix3x/pmix/contrib/perf_tools/pmi_intra_perf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ int get_mem_usage(double *_pss, double *_rss) {
129129
memset(data, 0, sizeof(data));
130130
snprintf(data, sizeof(data), "/proc/%d/smaps", pid);
131131

132-
if (NULL == (smaps = fopen(data, "r"))) {
132+
if (!access(data, F_OK) || NULL == (smaps = fopen(data, "r"))) {
133133
return -1;
134134
}
135135

opal/mca/pmix/pmix3x/pmix/src/mca/pif/linux_ipv6/pif_linux_ipv6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pmix_pif_base_component_t mca_pif_linux_ipv6_component = {
9494
static int if_linux_ipv6_open(void)
9595
{
9696
FILE *f;
97-
if ((f = fopen("/proc/net/if_inet6", "r"))) {
97+
if (access("/proc/net/if_inet6", F_OK) && NULL != (f = fopen("/proc/net/if_inet6", "r"))) {
9898
char ifname[IF_NAMESIZE];
9999
unsigned int idx, pfxlen, scope, dadstat;
100100
struct in6_addr a6;

opal/mca/pmix/pmix3x/pmix/src/mca/ptl/tcp/ptl_tcp.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,7 @@ static pmix_status_t parse_uri_file(char *filename,
471471
struct timeval tv;
472472
int retries;
473473

474-
fp = fopen(filename, "r");
475-
if (NULL == fp) {
474+
if (!access(filename, F_OK) || NULL == (fp = fopen(filename, "r"))) {
476475
/* if we cannot open the file, then the server must not
477476
* be configured to support tool connections, or this
478477
* user isn't authorized to access it - or it may just
@@ -495,8 +494,7 @@ static pmix_status_t parse_uri_file(char *filename,
495494
pmix_event_evtimer_add(&ev, &tv);
496495
PMIX_WAIT_THREAD(&lock);
497496
PMIX_DESTRUCT_LOCK(&lock);
498-
fp = fopen(filename, "r");
499-
if (NULL != fp) {
497+
if (access(filename, F_OK) && NULL != (fp = fopen(filename, "r"))) {
500498
/* we found it! */
501499
goto process;
502500
}

opal/mca/pmix/pmix3x/pmix/src/util/keyval_parse.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "src/util/output.h"
2929
#include <string.h>
3030
#include <ctype.h>
31+
#include <unistd.h>
3132

3233
int pmix_util_keyval_parse_lineno = 0;
3334

@@ -70,8 +71,7 @@ pmix_util_keyval_parse(const char *filename,
7071
keyval_callback = callback;
7172

7273
/* Open the pmix */
73-
pmix_util_keyval_yyin = fopen(keyval_filename, "r");
74-
if (NULL == pmix_util_keyval_yyin) {
74+
if (!access(keyval_filename, F_OK) || NULL == (pmix_util_keyval_yyin = fopen(keyval_filename, "r"))) {
7575
ret = PMIX_ERR_NOT_FOUND;
7676
goto cleanup;
7777
}

opal/mca/pmix/pmix3x/pmix/src/util/show_help.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <string.h>
2727
#include <locale.h>
2828
#include <errno.h>
29+
#include <unistd.h>
2930

3031
#include "src/mca/pinstalldirs/pinstalldirs.h"
3132
#include "src/util/show_help.h"
@@ -170,6 +171,9 @@ static int open_file(const char *base, const char *topic)
170171
if (0 > asprintf(&filename, "%s%s%s.txt", search_dirs[i], PMIX_PATH_SEP, base)) {
171172
return PMIX_ERR_OUT_OF_RESOURCE;
172173
}
174+
if(!access(filename, F_OK)){
175+
return PMIX_ERR_NOT_FOUND;
176+
}
173177
pmix_show_help_yyin = fopen(filename, "r");
174178
}
175179
}

opal/mca/pstat/linux/pstat_linux_module.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ static int query(pid_t pid,
289289
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
290290
}
291291

292-
if (NULL == (fp = fopen(data, "r"))) {
292+
if (!access(data, F_OK) || NULL == (fp = fopen(data, "r"))) {
293293
/* ignore this */
294294
return OPAL_SUCCESS;
295295
}
@@ -318,7 +318,7 @@ static int query(pid_t pid,
318318
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
319319
}
320320

321-
if (NULL == (fp = fopen(data, "r"))) {
321+
if (!access(data, F_OK) || NULL == (fp = fopen(data, "r"))) {
322322
/* ignore this */
323323
return OPAL_SUCCESS;
324324
}
@@ -365,7 +365,7 @@ static int query(pid_t pid,
365365
nstats->la15 = strtof(eptr, NULL);
366366

367367
/* see if we can open the meminfo file */
368-
if (NULL == (fp = fopen("/proc/meminfo", "r"))) {
368+
if (!access("/proc/meminfo", F_OK) || NULL == (fp = fopen("/proc/meminfo", "r"))) {
369369
/* ignore this */
370370
goto diskstats;
371371
}
@@ -398,7 +398,7 @@ static int query(pid_t pid,
398398

399399
diskstats:
400400
/* look for the diskstats file */
401-
if (NULL == (fp = fopen("/proc/diskstats", "r"))) {
401+
if (!access("/proc/diskstats", F_OK) || NULL == (fp = fopen("/proc/diskstats", "r"))) {
402402
/* not an error if we don't find this one as it
403403
* isn't critical
404404
*/
@@ -441,7 +441,7 @@ static int query(pid_t pid,
441441

442442
netstats:
443443
/* look for the netstats file */
444-
if (NULL == (fp = fopen("/proc/net/dev", "r"))) {
444+
if (!access("/proc/net/dev", F_OK) || NULL == (fp = fopen("/proc/net/dev", "r"))) {
445445
/* not an error if we don't find this one as it
446446
* isn't critical
447447
*/

opal/mca/timer/linux/timer_linux_component.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "opal_config.h"
2727

2828
#include <string.h>
29+
#include <unistd.h>
2930

3031
#include "opal/mca/timer/timer.h"
3132
#include "opal/mca/timer/base/base.h"
@@ -110,6 +111,9 @@ static int opal_timer_linux_find_freq(void)
110111
int ret;
111112
char buf[1024];
112113

114+
if(!access("/proc/cpuinfo", F_OK)) {
115+
return OPAL_ERR_IN_ERRNO;
116+
}
113117
fp = fopen("/proc/cpuinfo", "r");
114118
if (NULL == fp) {
115119
return OPAL_ERR_IN_ERRNO;

opal/runtime/opal_cr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ static int extract_env_vars(int prev_pid, char * file_name)
987987
goto cleanup;
988988
}
989989

990-
if (NULL == (env_data = fopen(file_name, "r")) ) {
990+
if (!access(env_data, F_OK) || NULL == (env_data = fopen(file_name, "r")) ) {
991991
exit_status = OPAL_ERROR;
992992
goto cleanup;
993993
}

opal/tools/opal-restart/opal-restart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ main(int argc, char *argv[])
214214
opal_restart_globals.snapshot_loc,
215215
opal_restart_globals.snapshot_ref,
216216
opal_restart_globals.snapshot_metadata);
217-
if( NULL == (metadata = fopen(full_metadata_path, "r")) ) {
217+
if(!access(full_metadata_path, F_OK) || NULL == (metadata = fopen(full_metadata_path, "r")) ) {
218218
opal_show_help("help-opal-restart.txt", "invalid_metadata", true,
219219
opal_restart_globals.snapshot_metadata,
220220
full_metadata_path);
@@ -657,7 +657,7 @@ static int post_env_vars(int prev_pid, opal_crs_base_snapshot_t *snapshot)
657657
/*
658658
* Any directories that need to be created
659659
*/
660-
if( NULL == (snapshot->metadata = fopen(snapshot->metadata_filename, "r")) ) {
660+
if(!access(snapshot->metadata_filename, F_OK) || NULL == (snapshot->metadata = fopen(snapshot->metadata_filename, "r")) ) {
661661
opal_show_help("help-opal-restart.txt", "invalid_metadata", true,
662662
opal_restart_globals.snapshot_metadata,
663663
snapshot->metadata_filename);

opal/util/keyval_parse.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "opal/threads/mutex.h"
2929
#include <string.h>
3030
#include <ctype.h>
31+
#include <unistd.h>
3132

3233
int opal_util_keyval_parse_lineno = 0;
3334

@@ -76,6 +77,10 @@ opal_util_keyval_parse(const char *filename,
7677
keyval_filename = filename;
7778
keyval_callback = callback;
7879

80+
if (!access(keyval_filename, F_OK)) {
81+
ret = OPAL_ERR_NOT_FOUND;
82+
goto cleanup;
83+
}
7984
/* Open the opal */
8085
opal_util_keyval_yyin = fopen(keyval_filename, "r");
8186
if (NULL == opal_util_keyval_yyin) {

opal/util/show_help.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <string.h>
2626
#include <locale.h>
2727
#include <errno.h>
28+
#include <unistd.h>
2829

2930
#include "opal/mca/installdirs/installdirs.h"
3031
#include "opal/util/show_help.h"
@@ -158,13 +159,20 @@ static int open_file(const char *base, const char *topic)
158159
*/
159160
for (i=0; NULL != search_dirs[i]; i++) {
160161
filename = opal_os_path( false, search_dirs[i], base, NULL );
162+
if(!access(filename, F_OK)) {
163+
goto doesntexist;
164+
}
161165
opal_show_help_yyin = fopen(filename, "r");
162166
if (NULL == opal_show_help_yyin) {
167+
doesntexist:
163168
asprintf(&err_msg, "%s: %s", filename, strerror(errno));
164169
base_len = strlen(base);
165170
if (4 > base_len || 0 != strcmp(base + base_len - 4, ".txt")) {
166171
free(filename);
167172
asprintf(&filename, "%s%s%s.txt", search_dirs[i], OPAL_PATH_SEP, base);
173+
if(!access(filename, F_OK)) {
174+
break;
175+
}
168176
opal_show_help_yyin = fopen(filename, "r");
169177
}
170178
}

0 commit comments

Comments
 (0)