Skip to content

Commit 9495e1d

Browse files
committed
Manually merge PR163 to address a number of sandbox resource crashes by migrating legacy direct /dev/urandom use to os.urandom and a few file read+writes to the fileio helpers.
git-svn-id: svn+ssh://svn.code.sf.net/p/migrid/code/trunk@6178 b75ad72c-e7d7-11dd-a971-7dbc132099af
1 parent 438fb9f commit 9495e1d

File tree

5 files changed

+31
-47
lines changed

5 files changed

+31
-47
lines changed

mig/server/jobscriptgenerator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# jobscriptgenerator - dynamically generate job script right before job handout
7-
# Copyright (C) 2003-2021 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2024 The MiG Project lead by Brian Vinter
88
#
99
# This file is part of MiG.
1010
#
@@ -183,8 +183,8 @@ def create_job_script(
183183
# TODO: hexlify is an awfully space wasting URL-safe encoding.
184184
# We should just use something like the proposed secure method from
185185
# http://stackoverflow.com/a/23728630/2213647
186-
sessionid = hexlify(open('/dev/urandom').read(session_id_bytes))
187-
iosessionid = hexlify(open('/dev/urandom').read(session_id_bytes))
186+
sessionid = hexlify(os.urandom(session_id_bytes))
187+
iosessionid = hexlify(os.urandom(session_id_bytes))
188188
helper_dict_filename = os.path.join(configuration.resource_home,
189189
unique_resource_name,
190190
'empty_job_helper_dict.%s' % exe)
@@ -476,7 +476,7 @@ def create_arc_job(
476476
return (None, 'Error. empty job for ARC?')
477477

478478
# generate random session ID:
479-
sessionid = hexlify(open('/dev/urandom').read(session_id_bytes))
479+
sessionid = hexlify(os.urandom(session_id_bytes))
480480
logger.debug('session ID (for creating links): %s' % sessionid)
481481

482482
client_dir = client_id_dir(client_id)

mig/shared/confparser.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# confparser - parse resource configurations
7-
# Copyright (C) 2003-2021 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2024 The MiG Project lead by Brian Vinter
88
#
99
# This file is part of MiG.
1010
#
@@ -34,6 +34,7 @@
3434
from __future__ import absolute_import
3535

3636
from mig.shared.conf import get_configuration_object
37+
from mig.shared.fileio import write_file
3738
from mig.shared.parser import parse, check_types
3839
from mig.shared.refunctions import is_runtime_environment, get_re_dict
3940
from mig.shared.resconfkeywords import get_keywords_dict as \
@@ -77,6 +78,7 @@ def run(configuration, localfile_spaces, unique_resource_name,
7778

7879
if not configuration:
7980
configuration = get_configuration_object()
81+
_logger = configuration.logger
8082

8183
(status, msg, conf) = get_resource_config_dict(configuration,
8284
localfile_spaces)
@@ -230,12 +232,6 @@ def run(configuration, localfile_spaces, unique_resource_name,
230232
else:
231233
return (True, 'Everything ok')
232234

233-
try:
234-
fsock = open(filename, 'w')
235-
st = dumps(conf, 0)
236-
fsock.write(st)
237-
fsock.close()
238-
except Exception as err:
239-
return (False, "Fatal error: could not open %r for writing!\n Msg: %s"
240-
% (filename, err))
235+
if not write_file(dumps(conf, 0), filename, _logger):
236+
return (False, "Fatal error: could not open %r for writing!" % filename)
241237
return (True, 'Everything ok, config updated')

mig/shared/functionality/ssscreateimg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# ssscreateimg - Back end to SSS zip generator
7-
# Copyright (C) 2003-2023 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2024 The MiG Project lead by Brian Vinter
88
#
99
# This file is part of MiG.
1010
#
@@ -93,7 +93,7 @@ def main(client_id, user_arguments_dict):
9393
win_solution = accepted['win_solution'][-1]
9494
vgrid_list = accepted['vgrid']
9595
cputime = 1000000
96-
sandboxkey = hexlify(open('/dev/urandom').read(32))
96+
sandboxkey = hexlify(os.urandom(32))
9797
ip_address = 'UNKNOWN'
9898
if 'REMOTE_ADDR' in os.environ:
9999
ip_address = os.environ['REMOTE_ADDR']

mig/shared/resource.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# resource - resource configuration functions
7-
# Copyright (C) 2003-2023 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2024 The MiG Project lead by Brian Vinter
88
#
99
# This file is part of MiG.
1010
#
@@ -50,7 +50,8 @@
5050
from mig.shared.base import client_id_dir
5151
from mig.shared.confparser import get_resource_config_dict, run
5252
from mig.shared.defaults import exe_leader_name, keyword_auto
53-
from mig.shared.fileio import pickle, move, walk
53+
from mig.shared.fileio import pickle, move, walk, write_file, read_file_lines, \
54+
write_file_lines
5455
from mig.shared.modified import mark_resource_modified, mark_vgrid_modified
5556
from mig.shared.pwcrypto import make_simple_hash
5657
from mig.shared.resconfkeywords import get_resource_specs, get_exenode_specs, \
@@ -827,6 +828,7 @@ def empty_resource_config(configuration):
827828
def write_resource_config(configuration, resource_conf, conf_path):
828829
"""Write resource_conf dictionary settings into conf_path on disk"""
829830

831+
_logger = configuration.logger
830832
lines = []
831833
for (field, __) in get_resource_specs(configuration):
832834
value = resource_conf.get(field, None)
@@ -867,9 +869,7 @@ def write_resource_config(configuration, resource_conf, conf_path):
867869
if not os.path.isdir(os.path.dirname(conf_path)):
868870
os.makedirs(os.path.dirname(conf_path))
869871

870-
conf_fd = open(conf_path, 'w')
871-
conf_fd.write('\n'.join(lines))
872-
conf_fd.close()
872+
write_file('\n'.join(lines), conf_path, _logger)
873873

874874
return lines
875875

@@ -1039,6 +1039,7 @@ def create_resource_conf(
10391039
relative path it will prefixed with the resource_pending dir of the
10401040
client_id.
10411041
"""
1042+
_logger = configuration.logger
10421043
if new_resource:
10431044
msg = """
10441045
Trying to create configuration for new resource: '%s.%d' from file '%s':
@@ -1078,7 +1079,7 @@ def create_resource_conf(
10781079
"""
10791080
Failure:
10801081
resource_name: '%s'
1081-
does'nt match hosturl: '%s'
1082+
doesn't match hosturl: '%s'
10821083
in configfile: '%s'"""\
10831084
% (resource_name, config_dict['HOSTURL'], pending_file)
10841085
return (False, msg)
@@ -1095,21 +1096,13 @@ def create_resource_conf(
10951096
pending_file)
10961097
return (False, msg)
10971098

1098-
try:
1099-
fr = open(pending_file, 'r')
1100-
fw = open(tmpfile, 'w')
1101-
readline = fr.readline()
1102-
while len(readline) > 0:
1103-
fw.write(readline.replace(keyword_auto, "%d" %
1104-
resource_identifier))
1105-
readline = fr.readline()
1106-
fw.close()
1107-
fr.close()
1108-
except Exception as err:
1109-
1110-
msg += \
1111-
'Failed to apply hostidentifier to configfile. Failure: %s'\
1112-
% err
1099+
pending_lines = read_file_lines(pending_file, _logger)
1100+
replaced_lines = []
1101+
for line in pending_lines:
1102+
replaced_lines.append(line.replace(keyword_auto, "%d" %
1103+
resource_identifier))
1104+
if not write_file_lines(replaced_lines, tmpfile, _logger):
1105+
msg += 'Failed to apply hostidentifier to configfile.'
11131106
return (False, msg)
11141107

11151108
unique_resource_name = "%s.%d" % (resource_name, resource_identifier)

mig/shared/sandbox.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# sandbox - shared sandbox helpers
7-
# Copyright (C) 2003-2021 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2024 The MiG Project lead by Brian Vinter
88
#
99
# This file is part of MiG.
1010
#
@@ -35,7 +35,7 @@
3535
from mig.shared.base import hexlify
3636
from mig.shared.conf import get_configuration_object
3737
from mig.shared.defaults import default_vgrid, keyword_auto
38-
from mig.shared.fileio import make_symlink
38+
from mig.shared.fileio import make_symlink, write_named_tempfile
3939
from mig.shared.resource import create_resource
4040
from mig.shared.serial import load, dump
4141

@@ -173,10 +173,7 @@ def create_oneclick_resource(
173173
# write the conf string to a temporary conf file
174174
# create_resource removes the tempfile automatically
175175

176-
tmp_file = tempfile.NamedTemporaryFile(delete=False)
177-
tmp_file.write(res_conf_string)
178-
tmp_file.close()
179-
pending_file = tmp_file.name
176+
pending_file = write_named_tempfile(configuration, res_conf_string)
180177

181178
(status, id_msg) = create_resource(configuration, sandboxkey,
182179
resource_name, pending_file)
@@ -190,6 +187,7 @@ def create_oneclick_resource(
190187

191188
exe_pgid_file = configuration.resource_home + unique_resource_name\
192189
+ os.sep + 'EXE_%s.PGID' % exe_name
190+
193191
try:
194192
fd = open(exe_pgid_file, 'w')
195193
fd.write('stopped')
@@ -314,10 +312,7 @@ def create_sss_resource(
314312
# write the conf string to a temporary conf file
315313
# create_resource removes the tempfile automatically
316314

317-
tmp_file = tempfile.NamedTemporaryFile(delete=False)
318-
tmp_file.write(res_conf_string)
319-
tmp_file.close()
320-
pending_file = tmp_file.name
315+
pending_file = write_named_tempfile(configuration, res_conf_string)
321316

322317
(status, id_msg) = create_resource(configuration, sandboxkey,
323318
resource_name, pending_file)
@@ -368,7 +363,7 @@ def get_resource(client_id, configuration, logger):
368363

369364
# Generate key, and set cookie
370365

371-
sandboxkey = hexlify(open('/dev/urandom').read(32))
366+
sandboxkey = hexlify(os.urandom(32))
372367
cookie = 'Set-Cookie: ' + __MIG_ONECLICK_COOKIE__ + '='\
373368
+ sandboxkey + '; '\
374369
+ 'expires=Thu 31-Jan-2099 12:00:00 GMT; path=/; '\

0 commit comments

Comments
 (0)