mirror of
https://github.com/restic/restic.git
synced 2025-12-08 06:09:56 +00:00
* Fix: Correctly restore ACL inheritance state When restoring a file or directory on Windows, the `IsInherited` property of its Access Control Entries (ACEs) was always being set to `False`, even if the ACEs were inherited in the original backup. This was caused by the restore process calling the `SetNamedSecurityInfo` API without providing context about the object's inheritance policy. By default, this API applies the provided Discretionary Access Control List (DACL) as an explicit set of permissions, thereby losing the original inheritance state. This commit fixes the issue by inspecting the `Control` flags of the saved Security Descriptor during restore. Based on whether the `SE_DACL_PROTECTED` flag is present, the code now adds the appropriate `PROTECTED_DACL_SECURITY_INFORMATION` or `UNPROTECTED_DACL_SECURITY_INFORMATION` flag to the `SetNamedSecurityInfo` API call. By providing this crucial inheritance context, the Windows API can now correctly reconstruct the ACL, ensuring the `IsInherited` status of each ACE is preserved as it was at the time of backup. * Fix: Correctly restore ACL inheritance flags This commit resolves an issue where the ACL inheritance state (`IsInherited` property) was not being correctly restored for files and directories on Windows. The root cause was that the `SECURITY_INFORMATION` flags used in the `SetNamedSecurityInfo` API call contained both the `PROTECTED_DACL_SECURITY_INFORMATION` and `UNPROTECTED_DACL_SECURITY_INFORMATION` flags simultaneously. When faced with this conflicting information, the Windows API defaulted to the more restrictive `PROTECTED` behavior, incorrectly disabling inheritance on restored items. The fix modifies the `setNamedSecurityInfoHigh` function to first clear all existing inheritance-related flags from the `securityInfo` bitmask. It then adds the single, correct flag (`PROTECTED` or `UNPROTECTED`) based on the `SE_DACL_PROTECTED` control bit from the original, saved Security Descriptor. This ensures that the API receives unambiguous instructions, allowing it to correctly preserve the inheritance state as it was at the time of backup. The accompanying test case for ACL inheritance now passes with this change. * Fix inheritance flag handling in low-privilege security descriptor restore When restoring files without admin privileges, the IsInherited property of Access Control Entries (ACEs) was not being preserved correctly. The low-privilege restore path (setNamedSecurityInfoLow) was using a static PROTECTED_DACL_SECURITY_INFORMATION flag, which always marked the restored DACL as explicitly set rather than inherited. This commit updates setNamedSecurityInfoLow to dynamically determine the correct inheritance flag based on the SE_DACL_PROTECTED control flag from the original security descriptor, matching the behavior of the high-privilege path (setNamedSecurityInfoHigh). Changes: - Update setNamedSecurityInfoLow to accept control flags parameter - Add logic to set either PROTECTED_DACL_SECURITY_INFORMATION or UNPROTECTED_DACL_SECURITY_INFORMATION based on the original SD - Add TestRestoreSecurityDescriptorInheritanceLowPrivilege to verify inheritance is correctly restored in low-privilege scenarios This ensures that both admin and non-admin restore operations correctly preserve the inheritance state of ACLs, maintaining the original permissions flow on child objects. Addresses review feedback on PR for issue #5427 * Refactor security flags into separate backup/restore variants Split highSecurityFlags into highBackupSecurityFlags and highRestoreSecurityFlags to avoid runtime bitwise operations. This makes the code cleaner and more maintainable by using appropriate flags for GET vs SET operations. Addresses review feedback on PR for issue #5427 --------- Co-authored-by: Aneesh Nireshwalia <anireshw@akamai.com>
212 lines
9.7 KiB
Go
212 lines
9.7 KiB
Go
package fs
|
|
|
|
import (
|
|
"fmt"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
var lowerPrivileges atomic.Bool
|
|
|
|
// Flags for backup with admin permissions. Includes protection flags for GET operations.
|
|
var highBackupSecurityFlags windows.SECURITY_INFORMATION = windows.OWNER_SECURITY_INFORMATION | windows.GROUP_SECURITY_INFORMATION | windows.DACL_SECURITY_INFORMATION | windows.SACL_SECURITY_INFORMATION | windows.LABEL_SECURITY_INFORMATION | windows.ATTRIBUTE_SECURITY_INFORMATION | windows.SCOPE_SECURITY_INFORMATION | windows.BACKUP_SECURITY_INFORMATION | windows.PROTECTED_DACL_SECURITY_INFORMATION | windows.PROTECTED_SACL_SECURITY_INFORMATION | windows.UNPROTECTED_DACL_SECURITY_INFORMATION | windows.UNPROTECTED_SACL_SECURITY_INFORMATION
|
|
|
|
// Flags for restore with admin permissions. Base flags without protection flags for SET operations.
|
|
var highRestoreSecurityFlags windows.SECURITY_INFORMATION = windows.OWNER_SECURITY_INFORMATION | windows.GROUP_SECURITY_INFORMATION | windows.DACL_SECURITY_INFORMATION | windows.SACL_SECURITY_INFORMATION | windows.LABEL_SECURITY_INFORMATION | windows.ATTRIBUTE_SECURITY_INFORMATION | windows.SCOPE_SECURITY_INFORMATION | windows.BACKUP_SECURITY_INFORMATION
|
|
|
|
// Flags for backup without admin permissions. If there are no admin permissions, only the current user's owner, group and DACL will be backed up.
|
|
var lowBackupSecurityFlags windows.SECURITY_INFORMATION = windows.OWNER_SECURITY_INFORMATION | windows.GROUP_SECURITY_INFORMATION | windows.DACL_SECURITY_INFORMATION | windows.LABEL_SECURITY_INFORMATION | windows.ATTRIBUTE_SECURITY_INFORMATION | windows.SCOPE_SECURITY_INFORMATION | windows.PROTECTED_DACL_SECURITY_INFORMATION | windows.UNPROTECTED_DACL_SECURITY_INFORMATION
|
|
|
|
// Flags for restore without admin permissions. If there are no admin permissions, only the DACL from the SD can be restored and owner and group will be set based on the current user.
|
|
var lowRestoreSecurityFlags windows.SECURITY_INFORMATION = windows.DACL_SECURITY_INFORMATION | windows.ATTRIBUTE_SECURITY_INFORMATION
|
|
|
|
// getSecurityDescriptor takes the path of the file and returns the SecurityDescriptor for the file.
|
|
// This needs admin permissions or SeBackupPrivilege for getting the full SD.
|
|
// If there are no admin permissions, only the current user's owner, group and DACL will be got.
|
|
func getSecurityDescriptor(filePath string) (securityDescriptor *[]byte, err error) {
|
|
var sd *windows.SECURITY_DESCRIPTOR
|
|
|
|
// store original value to avoid unrelated changes in the error check
|
|
useLowerPrivileges := lowerPrivileges.Load()
|
|
if useLowerPrivileges {
|
|
sd, err = getNamedSecurityInfoLow(filePath)
|
|
} else {
|
|
sd, err = getNamedSecurityInfoHigh(filePath)
|
|
// Fallback to the low privilege version when receiving an access denied error.
|
|
// For some reason the ERROR_PRIVILEGE_NOT_HELD error is not returned for removable media
|
|
// but instead an access denied error is returned. Workaround that by just retrying with
|
|
// the low privilege version, but don't switch privileges as we cannot distinguish this
|
|
// case from actual access denied errors.
|
|
// see https://github.com/restic/restic/issues/5003#issuecomment-2452314191 for details
|
|
if err != nil && isAccessDeniedError(err) {
|
|
sd, err = getNamedSecurityInfoLow(filePath)
|
|
}
|
|
}
|
|
if err != nil {
|
|
if !useLowerPrivileges && isHandlePrivilegeNotHeldError(err) {
|
|
// If ERROR_PRIVILEGE_NOT_HELD is encountered, fallback to backups/restores using lower non-admin privileges.
|
|
lowerPrivileges.Store(true)
|
|
return getSecurityDescriptor(filePath)
|
|
} else if errors.Is(err, windows.ERROR_NOT_SUPPORTED) {
|
|
return nil, nil
|
|
} else {
|
|
return nil, fmt.Errorf("get named security info failed with: %w", err)
|
|
}
|
|
}
|
|
|
|
sdBytes, err := securityDescriptorStructToBytes(sd)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("convert security descriptor to bytes failed: %w", err)
|
|
}
|
|
return &sdBytes, nil
|
|
}
|
|
|
|
// setSecurityDescriptor sets the SecurityDescriptor for the file at the specified path.
|
|
// This needs admin permissions or SeRestorePrivilege, SeSecurityPrivilege and SeTakeOwnershipPrivilege
|
|
// for setting the full SD.
|
|
// If there are no admin permissions/required privileges, only the DACL from the SD can be set and
|
|
// owner and group will be set based on the current user.
|
|
func setSecurityDescriptor(filePath string, securityDescriptor *[]byte) error {
|
|
// Set the security descriptor on the file
|
|
sd, err := securityDescriptorBytesToStruct(*securityDescriptor)
|
|
if err != nil {
|
|
return fmt.Errorf("error converting bytes to security descriptor: %w", err)
|
|
}
|
|
|
|
owner, _, err := sd.Owner()
|
|
if err != nil {
|
|
//Do not set partial values.
|
|
owner = nil
|
|
}
|
|
group, _, err := sd.Group()
|
|
if err != nil {
|
|
//Do not set partial values.
|
|
group = nil
|
|
}
|
|
dacl, _, err := sd.DACL()
|
|
if err != nil {
|
|
//Do not set partial values.
|
|
dacl = nil
|
|
}
|
|
sacl, _, err := sd.SACL()
|
|
if err != nil {
|
|
//Do not set partial values.
|
|
sacl = nil
|
|
}
|
|
|
|
// Get the control flags from the original security descriptor
|
|
control, _, err := sd.Control()
|
|
if err != nil {
|
|
// This is unlikely to fail if the sd is valid, but handle it.
|
|
return fmt.Errorf("could not get security descriptor control flags: %w", err)
|
|
}
|
|
// store original value to avoid unrelated changes in the error check
|
|
useLowerPrivileges := lowerPrivileges.Load()
|
|
if useLowerPrivileges {
|
|
err = setNamedSecurityInfoLow(filePath, dacl, control)
|
|
} else {
|
|
err = setNamedSecurityInfoHigh(filePath, owner, group, dacl, sacl, control)
|
|
// See corresponding fallback in getSecurityDescriptor for an explanation
|
|
if err != nil && isAccessDeniedError(err) {
|
|
err = setNamedSecurityInfoLow(filePath, dacl, control)
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
if !useLowerPrivileges && isHandlePrivilegeNotHeldError(err) {
|
|
// If ERROR_PRIVILEGE_NOT_HELD is encountered, fallback to backups/restores using lower non-admin privileges.
|
|
lowerPrivileges.Store(true)
|
|
return setSecurityDescriptor(filePath, securityDescriptor)
|
|
} else {
|
|
return fmt.Errorf("set named security info failed with: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// getNamedSecurityInfoHigh gets the higher level SecurityDescriptor which requires admin permissions.
|
|
func getNamedSecurityInfoHigh(filePath string) (*windows.SECURITY_DESCRIPTOR, error) {
|
|
return windows.GetNamedSecurityInfo(fixpath(filePath), windows.SE_FILE_OBJECT, highBackupSecurityFlags)
|
|
}
|
|
|
|
// getNamedSecurityInfoLow gets the lower level SecurityDescriptor which requires no admin permissions.
|
|
func getNamedSecurityInfoLow(filePath string) (*windows.SECURITY_DESCRIPTOR, error) {
|
|
return windows.GetNamedSecurityInfo(fixpath(filePath), windows.SE_FILE_OBJECT, lowBackupSecurityFlags)
|
|
}
|
|
|
|
// setNamedSecurityInfoHigh sets the higher level SecurityDescriptor which requires admin permissions.
|
|
func setNamedSecurityInfoHigh(filePath string, owner *windows.SID, group *windows.SID, dacl *windows.ACL, sacl *windows.ACL, control windows.SECURITY_DESCRIPTOR_CONTROL) error {
|
|
securityInfo := highRestoreSecurityFlags
|
|
|
|
// Check if the original DACL was protected from inheritance and add the correct flag.
|
|
if control&windows.SE_DACL_PROTECTED != 0 {
|
|
securityInfo |= windows.PROTECTED_DACL_SECURITY_INFORMATION
|
|
} else {
|
|
// Explicitly state that it is NOT protected. This ensures inheritance is re-enabled correctly.
|
|
securityInfo |= windows.UNPROTECTED_DACL_SECURITY_INFORMATION
|
|
}
|
|
|
|
// Do the same for the SACL for completeness.
|
|
if control&windows.SE_SACL_PROTECTED != 0 {
|
|
securityInfo |= windows.PROTECTED_SACL_SECURITY_INFORMATION
|
|
} else {
|
|
securityInfo |= windows.UNPROTECTED_SACL_SECURITY_INFORMATION
|
|
}
|
|
|
|
return windows.SetNamedSecurityInfo(fixpath(filePath), windows.SE_FILE_OBJECT, securityInfo, owner, group, dacl, sacl)
|
|
}
|
|
|
|
// setNamedSecurityInfoLow sets the lower level SecurityDescriptor which requires no admin permissions.
|
|
func setNamedSecurityInfoLow(filePath string, dacl *windows.ACL, control windows.SECURITY_DESCRIPTOR_CONTROL) error {
|
|
securityInfo := lowRestoreSecurityFlags
|
|
|
|
// Check if the original DACL was protected from inheritance and add the correct flag.
|
|
if control&windows.SE_DACL_PROTECTED != 0 {
|
|
securityInfo |= windows.PROTECTED_DACL_SECURITY_INFORMATION
|
|
} else {
|
|
// Explicitly state that it is NOT protected. This ensures inheritance is re-enabled correctly.
|
|
securityInfo |= windows.UNPROTECTED_DACL_SECURITY_INFORMATION
|
|
}
|
|
|
|
return windows.SetNamedSecurityInfo(fixpath(filePath), windows.SE_FILE_OBJECT, securityInfo, nil, nil, dacl, nil)
|
|
}
|
|
|
|
// isHandlePrivilegeNotHeldError checks if the error is ERROR_PRIVILEGE_NOT_HELD
|
|
func isHandlePrivilegeNotHeldError(err error) bool {
|
|
// Use a type assertion to check if the error is of type syscall.Errno
|
|
if errno, ok := err.(syscall.Errno); ok {
|
|
// Compare the error code to the expected value
|
|
return errno == windows.ERROR_PRIVILEGE_NOT_HELD
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isAccessDeniedError checks if the error is ERROR_ACCESS_DENIED
|
|
func isAccessDeniedError(err error) bool {
|
|
if errno, ok := err.(syscall.Errno); ok {
|
|
// Compare the error code to the expected value
|
|
return errno == windows.ERROR_ACCESS_DENIED
|
|
}
|
|
return false
|
|
}
|
|
|
|
// securityDescriptorBytesToStruct converts the security descriptor bytes representation
|
|
// into a pointer to windows SECURITY_DESCRIPTOR.
|
|
func securityDescriptorBytesToStruct(sd []byte) (*windows.SECURITY_DESCRIPTOR, error) {
|
|
if l := int(unsafe.Sizeof(windows.SECURITY_DESCRIPTOR{})); len(sd) < l {
|
|
return nil, fmt.Errorf("securityDescriptor (%d) smaller than expected (%d): %w", len(sd), l, windows.ERROR_INCORRECT_SIZE)
|
|
}
|
|
s := (*windows.SECURITY_DESCRIPTOR)(unsafe.Pointer(&sd[0]))
|
|
return s, nil
|
|
}
|
|
|
|
// securityDescriptorStructToBytes converts the pointer to windows SECURITY_DESCRIPTOR
|
|
// into a security descriptor bytes representation.
|
|
func securityDescriptorStructToBytes(sd *windows.SECURITY_DESCRIPTOR) ([]byte, error) {
|
|
b := unsafe.Slice((*byte)(unsafe.Pointer(sd)), sd.Length())
|
|
return b, nil
|
|
}
|