github.com/zaolin/u-root@v0.0.0-20200428085104-64aaafd46c6d/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 "golang.org/x/sys/unix"
     8  
     9  var (
    10  	fileSystemUID, fileSystemGID int
    11  )
    12  
    13  func dropPrivs() error {
    14  	uid := unix.Getuid()
    15  	if uid == 0 {
    16  		return nil
    17  	}
    18  
    19  	var err error
    20  	fileSystemUID, err = unix.SetfsuidRetUid(uid)
    21  	if err != nil {
    22  		return err
    23  	}
    24  	fileSystemGID, err = unix.SetfsgidRetGid(unix.Getgid())
    25  	return err
    26  }
    27  
    28  func restorePrivs() {
    29  	if unix.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  }