summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2021-07-07 11:08:41 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2021-07-20 19:05:45 +0100
commita7d713952e20ab181d3243a6b5af008bce786973 (patch)
tree53a522f8d7d1aab261a6b277912ae7874b404355 /scripts
parentdb00b0a059013a19b333fd291bde8cee71fc6a11 (diff)
downloadpoky-a7d713952e20ab181d3243a6b5af008bce786973.tar.gz
runqemu: Remove potential lock races around tap device handling
The qemu tap device handling is potentially race ridden. We pass the fd to the main qemu subprocess which is good as it means the lock is held as long as the qemu process exists. This means we shouldn't unlock it ourselves though, only close the file. We also can't delete the file as we have no idea if qemu is still using it. We could try and obtain an exclusive new lock, then the file would be safe to unlink but it doesn't seem worth it. Also fix the same issue in the port lock code. (From OE-Core rev: 61682a1a8bfe5f7be6e271a4143fa4a02ca5dca9) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org> (cherry picked from commit 2a87bddabf816d09ec801e33972879e6983627eb) Signed-off-by: Anuj Mittal <anuj.mittal@intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/runqemu27
1 files changed, 18 insertions, 9 deletions
diff --git a/scripts/runqemu b/scripts/runqemu
index edd17d09c4..c985f4e75a 100755
--- a/scripts/runqemu
+++ b/scripts/runqemu
@@ -232,9 +232,12 @@ class BaseConfig(object):
232 def release_taplock(self): 232 def release_taplock(self):
233 if self.taplock_descriptor: 233 if self.taplock_descriptor:
234 logger.debug("Releasing lockfile for tap device '%s'" % self.tap) 234 logger.debug("Releasing lockfile for tap device '%s'" % self.tap)
235 fcntl.flock(self.taplock_descriptor, fcntl.LOCK_UN) 235 # We pass the fd to the qemu process and if we unlock here, it would unlock for
236 # that too. Therefore don't unlock, just close
237 # fcntl.flock(self.taplock_descriptor, fcntl.LOCK_UN)
236 self.taplock_descriptor.close() 238 self.taplock_descriptor.close()
237 os.remove(self.taplock) 239 # Removing the file is a potential race, don't do that either
240 # os.remove(self.taplock)
238 self.taplock_descriptor = None 241 self.taplock_descriptor = None
239 242
240 def check_free_port(self, host, port, lockdir): 243 def check_free_port(self, host, port, lockdir):
@@ -272,17 +275,23 @@ class BaseConfig(object):
272 275
273 def release_portlock(self, lockfile=None): 276 def release_portlock(self, lockfile=None):
274 if lockfile != None: 277 if lockfile != None:
275 logger.debug("Releasing lockfile '%s'" % lockfile) 278 logger.debug("Releasing lockfile '%s'" % lockfile)
276 fcntl.flock(self.portlocks[lockfile], fcntl.LOCK_UN) 279 # We pass the fd to the qemu process and if we unlock here, it would unlock for
277 self.portlocks[lockfile].close() 280 # that too. Therefore don't unlock, just close
278 os.remove(lockfile) 281 # fcntl.flock(self.portlocks[lockfile], fcntl.LOCK_UN)
279 del self.portlocks[lockfile] 282 self.portlocks[lockfile].close()
283 # Removing the file is a potential race, don't do that either
284 # os.remove(lockfile)
285 del self.portlocks[lockfile]
280 elif len(self.portlocks): 286 elif len(self.portlocks):
281 for lockfile, descriptor in self.portlocks.items(): 287 for lockfile, descriptor in self.portlocks.items():
282 logger.debug("Releasing lockfile '%s'" % lockfile) 288 logger.debug("Releasing lockfile '%s'" % lockfile)
283 fcntl.flock(descriptor, fcntl.LOCK_UN) 289 # We pass the fd to the qemu process and if we unlock here, it would unlock for
290 # that too. Therefore don't unlock, just close
291 # fcntl.flock(descriptor, fcntl.LOCK_UN)
284 descriptor.close() 292 descriptor.close()
285 os.remove(lockfile) 293 # Removing the file is a potential race, don't do that either
294 # os.remove(lockfile)
286 self.portlocks = {} 295 self.portlocks = {}
287 296
288 def get(self, key): 297 def get(self, key):