@@ -238,15 +238,11 @@ func (fs *FS) Create(path string, flags uint32, mode uint32, context *fuse.Conte
238
238
return nil , fuse .ToStatus (err )
239
239
}
240
240
defer syscall .Close (dirfd )
241
- // Don't set full mode before we have set the correct owner. Files with SUID/SGID
242
- // mode belonging to the wrong owner would be a security risk. Even for other
243
- // modes, we don't want anyone else to open the file in the meantime: the fd would
244
- // stay open and could later be used to read the file.
245
- origMode := mode
246
- if fs .args .PreserveOwner {
247
- mode = 0000
248
- }
249
241
fd := - 1
242
+ // Make sure context is nil if we don't want to preserve the owner
243
+ if ! fs .args .PreserveOwner {
244
+ context = nil
245
+ }
250
246
// Handle long file name
251
247
if ! fs .args .PlaintextNames && nametransform .IsLongContent (cName ) {
252
248
// Create ".name"
@@ -255,14 +251,14 @@ func (fs *FS) Create(path string, flags uint32, mode uint32, context *fuse.Conte
255
251
return nil , fuse .ToStatus (err )
256
252
}
257
253
// Create content
258
- fd , err = syscallcompat .Openat (dirfd , cName , newFlags | os .O_CREATE | os .O_EXCL , mode )
254
+ fd , err = syscallcompat .OpenatUser (dirfd , cName , newFlags | os .O_CREATE | os .O_EXCL , mode , context )
259
255
if err != nil {
260
256
nametransform .DeleteLongNameAt (dirfd , cName )
261
257
return nil , fuse .ToStatus (err )
262
258
}
263
259
} else {
264
260
// Create content, normal (short) file name
265
- fd , err = syscallcompat .Openat (dirfd , cName , newFlags | syscall .O_CREAT | syscall .O_EXCL , mode )
261
+ fd , err = syscallcompat .OpenatUser (dirfd , cName , newFlags | syscall .O_CREAT | syscall .O_EXCL , mode , context )
266
262
if err != nil {
267
263
// xfstests generic/488 triggers this
268
264
if err == syscall .EMFILE {
@@ -273,24 +269,6 @@ func (fs *FS) Create(path string, flags uint32, mode uint32, context *fuse.Conte
273
269
return nil , fuse .ToStatus (err )
274
270
}
275
271
}
276
- // Set owner
277
- if fs .args .PreserveOwner {
278
- err = syscall .Fchown (fd , int (context .Owner .Uid ), int (context .Owner .Gid ))
279
- if err != nil {
280
- tlog .Warn .Printf ("Create %q: Fchown %d:%d failed: %v" , cName , context .Owner .Uid , context .Owner .Gid , err )
281
- // In case of a failure, we don't want to proceed setting more
282
- // permissive modes.
283
- syscall .Close (fd )
284
- return nil , fuse .ToStatus (err )
285
- }
286
- }
287
- // Set mode
288
- if mode != origMode {
289
- err = syscall .Fchmod (fd , origMode )
290
- if err != nil {
291
- tlog .Warn .Printf ("Create %q: Fchmod %#o -> %#o failed: %v" , cName , mode , origMode , err )
292
- }
293
- }
294
272
f := os .NewFile (uintptr (fd ), cName )
295
273
return NewFile (f , fs )
296
274
}
0 commit comments