github.com/system-transparency/u-root@v6.0.1-0.20190919065413-ed07a650de4c+incompatible/cmds/core/fusermount/fusermount_linux.go (about)

     1  // Copyright 2018-2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  var (
    14  	fileSystemUID, fileSystemGID int
    15  )
    16  
    17  func dropPrivs() error {
    18  	if fileSystemUID = unix.Getuid(); fileSystemUID == 0 {
    19  		return nil
    20  	}
    21  	fileSystemGID = unix.Getgid()
    22  	if err := unix.Setfsuid(fileSystemUID); err != nil {
    23  		return err
    24  	}
    25  	return unix.Setfsgid(fileSystemGID)
    26  }
    27  
    28  func restorePrivs() {
    29  	if os.Getuid() == 0 {
    30  		return
    31  	}
    32  	// We're exiting, if there's an error, not much to do.
    33  	unix.Setfsuid(fileSystemUID)
    34  	unix.Setfsgid(fileSystemGID)
    35  }
    36  
    37  func preMount() error {
    38  	// I guess this umask is the thing to do.
    39  	unix.Umask(033)
    40  	return nil
    41  }