Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite linux/nfs_cleanup for security and multi-user, fixes #7938 #7939

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions plugins/hosts/linux/cap/nfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,25 @@ def self.nfs_prune(environment, ui, valid_ids)
def self.nfs_cleanup(id)
return if !File.exist?("/etc/exports")

user = Regexp.escape(Process.uid.to_s)
id = Regexp.escape(id.to_s)

# Only use "sudo" if we can't write to /etc/exports directly
sudo_command = ""
sudo_command = "sudo " if !File.writable?("/etc/exports")

# Use sed to just strip out the block of code which was inserted
# by Vagrant
tmp = ENV["TMPDIR"] || ENV["TMP"] || "/tmp"
system("cp /etc/exports '#{tmp}' && #{sudo_command}sed -r -e '\\\x01^# VAGRANT-BEGIN:( #{user})? #{id}\x01,\\\x01^# VAGRANT-END:( #{user})? #{id}\x01 d' -ibak '#{tmp}/exports' ; #{sudo_command}cp '#{tmp}/exports' /etc/exports")
# Strip out the block of code which was inserted by Vagrant
user = Regexp.escape(Process.uid.to_s)
id = Regexp.escape(id.to_s)
exports_in = File.read('/etc/exports')
exports_out = exports_in.gsub(%r{
^\#\ VAGRANT-BEGIN:((?:\ #{user})?\ #{id})$
.*?
^\#\ VAGRANT-END:\1$
\n?
}mx, '')
if exports_out != exports_in
open(%Q[|#{sudo_command}tee /etc/exports >/dev/null], 'w+') do |p|
p.write(exports_out)
end
end
end

def self.nfs_opts_setup(folders)
Expand Down