Skip to content

Commit 25931ea

Browse files
authored
Merge pull request #7200 from cpshereda/master-opal_gethostname-change
Fix unsafe use of gethostname()
2 parents 887400c + cbc6fea commit 25931ea

40 files changed

+386
-154
lines changed

ompi/info/info.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* Copyright (c) 2015-2018 Research Organization for Information Science
1818
* and Technology (RIST). All rights reserved.
1919
* Copyright (c) 2016-2017 IBM Corporation. All rights reserved.
20+
* Copyright (c) 2019 Triad National Security, LLC. All rights
21+
* reserved.
2022
* $COPYRIGHT$
2123
*
2224
* Additional copyrights may follow
@@ -40,6 +42,7 @@
4042
#endif
4143
#include <assert.h>
4244

45+
#include "opal/runtime/opal.h"
4346
#include "opal/util/argv.h"
4447
#include "opal/util/opal_getcwd.h"
4548
#include "opal/util/output.h"
@@ -82,7 +85,7 @@ opal_pointer_array_t ompi_info_f_to_c_table = {{0}};
8285
*/
8386
int ompi_mpiinfo_init(void)
8487
{
85-
char val[OPAL_MAXHOSTNAMELEN];
88+
const char *val;
8689
char *cptr;
8790

8891
/* initialize table */
@@ -121,7 +124,7 @@ int ompi_mpiinfo_init(void)
121124
}
122125

123126
/* local host name */
124-
gethostname(val, sizeof(val));
127+
val = opal_gethostname();
125128
opal_info_set(&ompi_mpi_info_env.info.super, "host", val);
126129

127130
/* architecture name */

ompi/mca/common/monitoring/common_monitoring.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Copyright (c) 2017-2018 Los Alamos National Security, LLC. All rights
1111
* reserved.
1212
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
13+
* Copyright (c) 2019 Triad National Security, LLC. All rights reserved.
1314
* $COPYRIGHT$
1415
*
1516
* Additional copyrights may follow
@@ -26,6 +27,7 @@
2627
#include <opal/class/opal_hash_table.h>
2728
#include <opal/util/output.h>
2829
#include "opal/util/printf.h"
30+
#include "opal/runtime/opal.h"
2931
#include <math.h>
3032

3133
#if SIZEOF_LONG_LONG == SIZEOF_SIZE_T
@@ -213,11 +215,11 @@ int mca_common_monitoring_init( void )
213215
if( !mca_common_monitoring_enabled ) return OMPI_ERROR;
214216
if( 1 < opal_atomic_add_fetch_32(&mca_common_monitoring_hold, 1) ) return OMPI_SUCCESS; /* Already initialized */
215217

216-
char hostname[OPAL_MAXHOSTNAMELEN] = "NA";
218+
const char *hostname;
217219
/* Initialize constant */
218220
log10_2 = log10(2.);
219221
/* Open the opal_output stream */
220-
gethostname(hostname, sizeof(hostname));
222+
hostname = opal_gethostname();
221223
opal_asprintf(&mca_common_monitoring_output_stream_obj.lds_prefix,
222224
"[%s:%06d] monitoring: ", hostname, getpid());
223225
mca_common_monitoring_output_stream_id =

ompi/mca/pml/v/pml_v_output.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* All rights reserved.
44
* Copyright (c) 2017 IBM Corporation. All rights reserved.
55
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
6+
* Copyright (c) 2019 Triad National Security, LLC. All rights
7+
* reserved.
68
* $COPYRIGHT$
79
*
810
* Additional copyrights may follow
@@ -12,6 +14,7 @@
1214

1315
#include "ompi_config.h"
1416

17+
#include "opal/runtime/opal.h"
1518
#include "opal/util/output.h"
1619
#include "opal/util/printf.h"
1720

@@ -24,7 +27,7 @@
2427

2528
int ompi_pml_v_output_open(char *output, int verbosity) {
2629
opal_output_stream_t lds;
27-
char hostname[OPAL_MAXHOSTNAMELEN] = "NA";
30+
const char *hostname;
2831

2932
OBJ_CONSTRUCT(&lds, opal_output_stream_t);
3033
if(!output) {
@@ -43,7 +46,7 @@ int ompi_pml_v_output_open(char *output, int verbosity) {
4346
lds.lds_file_suffix = output;
4447
}
4548
lds.lds_is_debugging = true;
46-
gethostname(hostname, sizeof(hostname));
49+
hostname = opal_gethostname();
4750
opal_asprintf(&lds.lds_prefix, "[%s:%05d] pml_v: ", hostname, getpid());
4851
lds.lds_verbose_level = verbosity;
4952
mca_pml_v.output = opal_output_open(&lds);

ompi/runtime/ompi_mpi_abort.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Copyright (c) 2015 Mellanox Technologies, Inc.
2020
* All rights reserved.
2121
* Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
22+
* Copyright (c) 2019 Triad National Security, LLC. All rights
23+
* reserved.
2224
* $COPYRIGHT$
2325
*
2426
* Additional copyrights may follow
@@ -42,6 +44,7 @@
4244
#endif
4345
#include <errno.h>
4446

47+
#include "opal/runtime/opal.h"
4548
#include "opal/mca/backtrace/backtrace.h"
4649
#include "opal/util/error.h"
4750
#include "opal/runtime/opal_params.h"
@@ -121,7 +124,7 @@ int
121124
ompi_mpi_abort(struct ompi_communicator_t* comm,
122125
int errcode)
123126
{
124-
char *host, hostname[OPAL_MAXHOSTNAMELEN];
127+
const char *host;
125128
pid_t pid = 0;
126129

127130
/* Protection for recursive invocation */
@@ -131,12 +134,11 @@ ompi_mpi_abort(struct ompi_communicator_t* comm,
131134
have_been_invoked = true;
132135

133136
/* If MPI is initialized, we know we have a runtime nodename, so
134-
use that. Otherwise, call gethostname. */
137+
use that. Otherwise, call opal_gethostname. */
135138
if (ompi_rte_initialized) {
136139
host = ompi_process_info.nodename;
137140
} else {
138-
gethostname(hostname, sizeof(hostname));
139-
host = hostname;
141+
host = opal_gethostname();
140142
}
141143
pid = getpid();
142144

ompi/runtime/ompi_mpi_finalize.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* and Technology (RIST). All rights reserved.
2222
*
2323
* Copyright (c) 2016-2017 IBM Corporation. All rights reserved.
24+
* Copyright (c) 2019 Triad National Security, LLC. All rights
25+
* reserved.
2426
* $COPYRIGHT$
2527
*
2628
* Additional copyrights may follow
@@ -119,9 +121,9 @@ int ompi_mpi_finalize(void)
119121
/* Note that if we're not initialized or already finalized, we
120122
cannot raise an MPI exception. The best that we can do is
121123
write something to stderr. */
122-
char hostname[OPAL_MAXHOSTNAMELEN];
124+
const char *hostname;
123125
pid_t pid = getpid();
124-
gethostname(hostname, sizeof(hostname));
126+
hostname = opal_gethostname();
125127

126128
if (state < OMPI_MPI_STATE_INIT_COMPLETED) {
127129
opal_show_help("help-mpi-runtime.txt",

ompi/tools/mpisync/sync.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* Copyright (C) 2014 Artem Polyakov <[email protected]>
33
* Copyright (c) 2014-2017 Intel, Inc. All rights reserved.
4+
* Copyright (c) 2019 Triad National Security, LLC. All rights
5+
* reserved.
46
* $COPYRIGHT$
57
*
68
* Additional copyrights may follow
@@ -9,6 +11,7 @@
911
*/
1012

1113
#include "opal_config.h"
14+
#include "opal/runtime/opal.h"
1215

1316
#include <stdio.h>
1417
#include <mpi.h>
@@ -77,7 +80,8 @@ int main(int argc, char **argv)
7780
int rank, commsize;
7881
double offs = 0, rtt = 0;
7982
char hname[OPAL_MAXHOSTNAMELEN];
80-
83+
const char *local_hname;
84+
8185
MPI_Comm_rank(comm, &rank);
8286
MPI_Comm_size(comm, &commsize);
8387

@@ -99,11 +103,20 @@ int main(int argc, char **argv)
99103
MPI_Finalize();
100104
exit(1);
101105
}
102-
103-
if( gethostname(hname, sizeof(hname)) ){
104-
perror("Cannot get hostname. Abort");
105-
MPI_Abort(MPI_COMM_WORLD, 1);
106-
}
106+
/* All error checking for getting the hostname is done with the initial
107+
populating of opal_process_info.nodename inside opal/runtime/opal_init.c */
108+
local_hname = opal_gethostname();
109+
110+
/* Truncate hostname if it is longer than OPAL_MAXHOSTNAMELEN,
111+
since we are only reading that length with the MPI_Gather.
112+
If full and complete hostnames are necessary in all cases,
113+
this could be implemented with MPI_Gatherv rather than
114+
MPI_Gather, instead of using the longer but more
115+
accurate OPAL_LOCAL_MAXHOSTNAMELEN value, because for very
116+
large task counts there is a risk of running out of memory
117+
if OPAL_LOCAL_MAXHOSTNAMELEN is used. */
118+
strncpy(hname, local_hname, OPAL_MAXHOSTNAMELEN - 1);
119+
hname[OPAL_MAXHOSTNAMELEN - 1] = '\0';
107120

108121
int rc = hpctimer_initialize("gettimeofday");
109122

opal/include/opal_config_bottom.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@
301301
#define OPAL_MAXHOSTNAMELEN (255 + 1)
302302
#endif
303303

304+
#define OPAL_LOCAL_MAXHOSTNAMELEN 4096
305+
306+
#if (OPAL_MAXHOSTNAMELEN > OPAL_LOCAL_MAXHOSTNAMELEN)
307+
#define OPAL_LOCAL_MAXHOSTNAMELEN OPAL_MAXHOSTNAMELEN
308+
#endif
309+
304310
/*
305311
* Do we want memory debugging?
306312
*

opal/mca/base/mca_base_component_find.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* and Technology (RIST). All rights reserved.
1717
* Copyright (c) 2014-2015 Los Alamos National Security, LLC. All rights
1818
* reserved.
19+
* Copyright (c) 2019 Triad National Security, LLC. All rights
20+
* reserved.
1921
* $COPYRIGHT$
2022
*
2123
* Additional copyrights may follow
@@ -45,6 +47,7 @@
4547
#include <netdb.h>
4648
#endif
4749

50+
#include "opal/runtime/opal.h"
4851
#include "opal/mca/installdirs/installdirs.h"
4952
#include "opal/util/opal_environ.h"
5053
#include "opal/util/output.h"
@@ -330,8 +333,8 @@ static int component_find_check (mca_base_framework_t *framework, char **request
330333
}
331334

332335
if (!found) {
333-
char h[OPAL_MAXHOSTNAMELEN];
334-
gethostname(h, sizeof(h));
336+
const char *h;
337+
h = opal_gethostname();
335338
opal_show_help("help-mca-base.txt",
336339
"find-available:not-valid", true,
337340
h, framework->framework_name, requested_component_names[i]);

opal/mca/base/mca_base_open.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* reserved.
1616
* Copyright (c) 2017 IBM Corporation. All rights reserved.
1717
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
18-
* Copyright (c) 2018 Triad National Security, LLC. All rights
18+
* Copyright (c) 2018-2019 Triad National Security, LLC. All rights
1919
* reserved.
2020
* $COPYRIGHT$
2121
*
@@ -73,7 +73,7 @@ int mca_base_open(void)
7373
{
7474
char *value;
7575
opal_output_stream_t lds;
76-
char hostname[OPAL_MAXHOSTNAMELEN];
76+
const char *hostname;
7777
int var_id;
7878

7979
if (mca_base_opened++) {
@@ -159,7 +159,7 @@ int mca_base_open(void)
159159
} else {
160160
set_defaults(&lds);
161161
}
162-
gethostname(hostname, sizeof(hostname));
162+
hostname = opal_gethostname();
163163
opal_asprintf(&lds.lds_prefix, "[%s:%05d] ", hostname, getpid());
164164
opal_output_reopen(0, &lds);
165165
opal_output_verbose (MCA_BASE_VERBOSE_COMPONENT, 0, "mca: base: opening components");

opal/mca/if/linux_ipv6/if_linux_ipv6.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
44
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights
55
* reserved.
6+
* Copyright (c) 2019 Triad National Security, LLC. All rights
7+
* reserved.
68
* $COPYRIGHT$
79
*
810
* Additional copyrights may follow
@@ -45,6 +47,7 @@
4547
#include <ifaddrs.h>
4648
#endif
4749

50+
#include "opal/runtime/opal.h"
4851
#include "opal/constants.h"
4952
#include "opal/util/if.h"
5053
#include "opal/util/output.h"
@@ -127,8 +130,8 @@ static int if_linux_ipv6_open(void)
127130
opal_if_t *intf;
128131

129132
if (!hexdecode(addrhex, a6.s6_addr, sizeof a6.s6_addr)) {
130-
char hostname[OPAL_MAXHOSTNAMELEN];
131-
gethostname(hostname, sizeof(hostname));
133+
const char *hostname;
134+
hostname = opal_gethostname();
132135
opal_show_help("help-opal-if-linux-ipv6.txt",
133136
"fail to parse if_inet6", true,
134137
hostname, ifname, addrhex);

0 commit comments

Comments
 (0)