Skip to content

Commit 41b71f9

Browse files
authored
Fix race condition in CFPreferences (#4903)
In __CFWriteBytesToFileWithAtomicity(), we first write the contents of the plist to an auxiliary copy file, and then move that copy to where the original used to be. Because that file needs to have the same owner as the original, we would use chown() to change ownership as the last step. This allows a race condition where the new file is in its final location, but doesn't have the correct permissions. To fix this, call chown() on the file before moving. rdar://121597642
1 parent 1dac113 commit 41b71f9

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

CoreFoundation/Preferences.subproj/CFXMLPreferencesDomain.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,17 @@ static Boolean __CFWriteBytesToFileWithAtomicity(CFURLRef url, const void *bytes
273273
close(fd);
274274

275275
if (atomic) {
276+
// If the file was renamed successfully and we wrote it as root we need to reset the owner & group as they were.
277+
if (writingFileAsRoot) {
278+
chown(auxPath, owner, group);
279+
}
276280
// preserve the mode as passed in originally
277281
chmod(auxPath, mode);
278282

279283
if (0 != rename(auxPath, cpath)) {
280284
unlink(auxPath);
281285
return false;
282286
}
283-
284-
// If the file was renamed successfully and we wrote it as root we need to reset the owner & group as they were.
285-
if (writingFileAsRoot) {
286-
chown(cpath, owner, group);
287-
}
288287
}
289288
return true;
290289
}

0 commit comments

Comments
 (0)