GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — locks/metadata_windows.go at master · asciimoth/locks

Expand file tree

/

metadata_windows.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
197 lines (190 loc) · 5.76 KB
/

metadata_windows.go

Copy path

File metadata and controls

197 lines (190 loc) · 5.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//go:build windows
package locks
import (
"encoding/binary"
"fmt"
"os"
"strings"
"unsafe"
"golang.org/x/sys/windows"
)
// Create the guard and staging objects with a protected DACL before writing
// any bytes. In particular a permissive inheritable ACE on the parent must
// never expose a staged copy of a private leaf.
func privateDescriptor(parent *os.File) (*windows.SECURITY_DESCRIPTOR, error) {
var flags uint32
if err := windows.GetVolumeInformationByHandle(windows.Handle(parent.Fd()), nil, 0, nil, nil, &flags, nil, 0); err != nil {
return nil, err
}
if flags&windows.FILE_PERSISTENT_ACLS == 0 {
return nil, fmt.Errorf("%w: volume lacks persistent ACLs", ErrUnsupported)
}
user, err := windows.GetCurrentThreadEffectiveToken().GetTokenUser()
if err != nil {
return nil, err
}
return windows.SecurityDescriptorFromString("D:P(A;;FA;;;" + user.User.Sid.String() + ")")
}
// FileMode controls new entries. Express owner, primary group and other bits
// as NT access masks. Writing a leaf also permits its rename during publication.
func modeDescriptor(sd *windows.SECURITY_DESCRIPTOR, mode uint32) (*windows.SECURITY_DESCRIPTOR, error) {
owner, _, err := sd.Owner()
if err != nil {
return nil, err
}
group, _, err := sd.Group()
if err != nil {
return nil, err
}
var masks [3]uint32
for i := range masks {
bits := (mode >> (6 - 3*i)) & 7
if bits&4 != 0 {
masks[i] |= windows.FILE_GENERIC_READ
}
if bits&2 != 0 {
masks[i] |= windows.FILE_GENERIC_WRITE | windows.DELETE
}
if bits&1 != 0 {
masks[i] |= windows.FILE_GENERIC_EXECUTE
}
}
var acl strings.Builder
acl.WriteString("D:P")
for i, sid := range []string{owner.String(), group.String(), "WD"} {
// A member of the owner or group class must not gain bits from a
// later, more permissive class (for example, mode 0646).
var later uint32
for _, mask := range masks[i+1:] {
later |= mask
}
if deny := later &^ masks[i]; deny != 0 {
fmt.Fprintf(&acl, "(D;;0x%x;;;%s)", deny, sid)
}
if masks[i] != 0 {
fmt.Fprintf(&acl, "(A;;0x%x;;;%s)", masks[i], sid)
}
}
return windows.SecurityDescriptorFromString(acl.String())
}
func setMetadata(src, dst *os.File, mode uint32, dir bool) error {
h := windows.Handle(dst.Fd())
const principals = windows.OWNER_SECURITY_INFORMATION | windows.GROUP_SECURITY_INFORMATION
target, err := windows.GetSecurityInfo(h, windows.SE_FILE_OBJECT, principals)
if err != nil {
return err
}
info := windows.SECURITY_INFORMATION(windows.DACL_SECURITY_INFORMATION | windows.PROTECTED_DACL_SECURITY_INFORMATION)
var sd *windows.SECURITY_DESCRIPTOR
if src == nil {
sd, err = modeDescriptor(target, mode)
if err != nil {
return err
}
} else {
// Including the mandatory label retains integrity restrictions when
// copying between directories with different inheritance settings.
sd, err = windows.GetSecurityInfo(windows.Handle(src.Fd()), windows.SE_FILE_OBJECT,
principals|windows.DACL_SECURITY_INFORMATION|windows.LABEL_SECURITY_INFORMATION)
if err != nil {
return err
}
// The low-level setter keeps AUTO_INHERITED only when the matching
// AUTO_INHERIT_REQ flag is also set in the supplied descriptor.
control, _, err := sd.Control()
if err != nil {
return err
}
if control&windows.SE_DACL_AUTO_INHERITED != 0 {
if err := sd.SetControl(windows.SE_DACL_AUTO_INHERIT_REQ, windows.SE_DACL_AUTO_INHERIT_REQ); err != nil {
return err
}
}
owner, _, err := sd.Owner()
if err != nil {
return err
}
group, _, err := sd.Group()
if err != nil {
return err
}
targetOwner, _, err := target.Owner()
if err != nil {
return err
}
targetGroup, _, err := target.Group()
if err != nil {
return err
}
if !owner.Equals(targetOwner) {
info |= windows.OWNER_SECURITY_INFORMATION
}
if !group.Equals(targetGroup) {
info |= windows.GROUP_SECURITY_INFORMATION
}
label, _, err := sd.SACL()
if err != nil && err != windows.ERROR_OBJECT_NOT_FOUND {
return err
}
if label != nil {
info |= windows.LABEL_SECURITY_INFORMATION
}
if dir {
dacl, _, err := sd.DACL()
if err != nil {
return err
}
if err = addDirectoryTraverse(dacl); err != nil {
return err
}
}
}
// Set by handle without recursively propagating a directory DACL to the
// already staged children, each of which has its own copy of the leaf ACL.
if err = windows.SetKernelObjectSecurity(h, info, sd); err != nil {
return err
}
if !dir && mode&0222 == 0 {
var basic struct {
Times [4]int64
Attributes uint32
Padding uint32
}
basic.Attributes = windows.FILE_ATTRIBUTE_READONLY
return windows.SetFileInformationByHandle(h, windows.FileBasicInfo, (*byte)(unsafe.Pointer(&basic)), uint32(unsafe.Sizeof(basic)))
}
return nil
}
// The ACL and ACE layouts are defined in winnt.h. The access mask follows
// the four-byte ACE_HEADER for simple, object and callback access ACEs.
// Carry both read grants and read denials over to traversal. Keep each ACE's
// SID, conditions and inheritance flags.
func addDirectoryTraverse(acl *windows.ACL) error {
if acl == nil {
return nil
}
h := unsafe.Slice((*byte)(unsafe.Pointer(acl)), 8)
size, count := int(binary.LittleEndian.Uint16(h[2:])), int(binary.LittleEndian.Uint16(h[4:]))
if size < 8 {
return ErrFormat
}
data := unsafe.Slice((*byte)(unsafe.Pointer(acl)), size)[8:]
for i := 0; i < count; i++ {
if len(data) < 8 {
return ErrFormat
}
n := int(binary.LittleEndian.Uint16(data[2:]))
if n < 8 || n > len(data) {
return ErrFormat
}
switch data[0] {
case 0, 1, 5, 6, 9, 10, 11, 12: // allow/deny, object and callback variants
rights := binary.LittleEndian.Uint32(data[4:])
if rights&windows.FILE_READ_DATA != 0 {
binary.LittleEndian.PutUint32(data[4:], rights|windows.FILE_TRAVERSE)
}
}
data = data[n:]
}
return nil
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.