Skip to content

Commit 355e5f2

Browse files
committed
Revert "binutils: Make smart_rename safe too"
This reverts commit 014cc7f. Given the problems associated with this patch and the others intended to fix the smart_rename CVE, the decision has been taken to$
1 parent a708e76 commit 355e5f2

File tree

5 files changed

+42
-130
lines changed

5 files changed

+42
-130
lines changed

binutils/ar.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,8 +1254,6 @@ write_archive (bfd *iarch)
12541254
char *old_name, *new_name;
12551255
bfd *contents_head = iarch->archive_next;
12561256
int ofd = -1;
1257-
struct stat target_stat;
1258-
bfd_boolean skip_stat = FALSE;
12591257

12601258
old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
12611259
strcpy (old_name, bfd_get_filename (iarch));
@@ -1301,14 +1299,6 @@ write_archive (bfd *iarch)
13011299
if (!bfd_set_archive_head (obfd, contents_head))
13021300
bfd_fatal (old_name);
13031301

1304-
#if !defined (_WIN32) || defined (__CYGWIN32__)
1305-
ofd = dup (ofd);
1306-
if (iarch == NULL || iarch->iostream == NULL)
1307-
skip_stat = TRUE;
1308-
else if (ofd == -1 || fstat (fileno (iarch->iostream), &target_stat) != 0)
1309-
bfd_fatal (old_name);
1310-
#endif
1311-
13121302
if (!bfd_close (obfd))
13131303
bfd_fatal (old_name);
13141304

@@ -1318,7 +1308,7 @@ write_archive (bfd *iarch)
13181308
/* We don't care if this fails; we might be creating the archive. */
13191309
bfd_close (iarch);
13201310

1321-
if (smart_rename (new_name, old_name, ofd, skip_stat ? NULL : &target_stat, 0) != 0)
1311+
if (smart_rename (new_name, old_name, 0) != 0)
13221312
xexit (1);
13231313
free (old_name);
13241314
free (new_name);

binutils/arsup.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,25 +345,13 @@ ar_save (void)
345345
else
346346
{
347347
char *ofilename = xstrdup (bfd_get_filename (obfd));
348-
bfd_boolean skip_stat = FALSE;
349-
struct stat target_stat;
350-
int ofd = -1;
351348

352349
if (deterministic > 0)
353350
obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
354351

355-
#if !defined (_WIN32) || defined (__CYGWIN32__)
356-
/* It's OK to fail; at worst it will result in SMART_RENAME using a slow
357-
copy fallback to write the output. */
358-
ofd = dup (fileno (obfd->iostream));
359-
if (lstat (real_name, &target_stat) != 0)
360-
skip_stat = TRUE;
361-
#endif
362-
363352
bfd_close (obfd);
364353

365-
smart_rename (ofilename, real_name, ofd,
366-
skip_stat ? NULL : &target_stat, 0);
354+
smart_rename (ofilename, real_name, 0);
367355
obfd = 0;
368356
free (ofilename);
369357
}

binutils/bucomm.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ extern void print_version (const char *);
7171
/* In rename.c. */
7272
extern void set_times (const char *, const struct stat *);
7373

74-
extern int smart_rename (const char *, const char *, int, struct stat *, int);
75-
74+
extern int smart_rename (const char *, const char *, int);
7675

7776
/* In libiberty. */
7877
void *xmalloc (size_t);

binutils/objcopy.c

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4835,7 +4835,6 @@ strip_main (int argc, char *argv[])
48354835
struct stat statbuf;
48364836
char *tmpname;
48374837
int tmpfd = -1;
4838-
int copyfd = -1;
48394838

48404839
if (get_file_size (argv[i]) < 1)
48414840
{
@@ -4849,12 +4848,7 @@ strip_main (int argc, char *argv[])
48494848
else
48504849
tmpname = output_file;
48514850

4852-
if (tmpname == NULL
4853-
#if !defined (_WIN32) || defined (__CYGWIN32__)
4854-
/* Retain a copy of TMPFD since we will need it for SMART_RENAME. */
4855-
|| (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
4856-
#endif
4857-
)
4851+
if (tmpname == NULL)
48584852
{
48594853
bfd_nonfatal_message (argv[i], NULL, NULL,
48604854
_("could not create temporary file to hold stripped copy"));
@@ -4872,18 +4866,12 @@ strip_main (int argc, char *argv[])
48724866
if (output_file != tmpname)
48734867
status = (smart_rename (tmpname,
48744868
output_file ? output_file : argv[i],
4875-
copyfd, &statbuf, preserve_dates) != 0);
4869+
preserve_dates) != 0);
48764870
if (status == 0)
48774871
status = hold_status;
48784872
}
48794873
else
4880-
{
4881-
#if !defined (_WIN32) || defined (__CYGWIN32__)
4882-
if (copyfd >= 0)
4883-
close (copyfd);
4884-
#endif
4885-
unlink_if_ordinary (tmpname);
4886-
}
4874+
unlink_if_ordinary (tmpname);
48874875
if (output_file != tmpname)
48884876
free (tmpname);
48894877
}
@@ -5091,7 +5079,6 @@ copy_main (int argc, char *argv[])
50915079
bfd_boolean use_globalize = FALSE;
50925080
bfd_boolean use_keep_global = FALSE;
50935081
int c, tmpfd = -1;
5094-
int copyfd = -1;
50955082
struct stat statbuf;
50965083
const bfd_arch_info_type *input_arch = NULL;
50975084

@@ -5936,16 +5923,9 @@ copy_main (int argc, char *argv[])
59365923
else
59375924
tmpname = output_filename;
59385925

5939-
if (tmpname == NULL
5940-
#if !defined (_WIN32) || defined (__CYGWIN32__)
5941-
/* Retain a copy of TMPFD since we will need it for SMART_RENAME. */
5942-
|| (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
5943-
#endif
5944-
)
5945-
{
5946-
fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
5947-
input_filename, strerror (errno));
5948-
}
5926+
if (tmpname == NULL)
5927+
fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
5928+
input_filename, strerror (errno));
59495929

59505930
copy_file (input_filename, tmpname, tmpfd, &statbuf, input_target,
59515931
output_target, input_arch);
@@ -5954,17 +5934,11 @@ copy_main (int argc, char *argv[])
59545934
if (preserve_dates)
59555935
set_times (tmpname, &statbuf);
59565936
if (tmpname != output_filename)
5957-
status = (smart_rename (tmpname, input_filename, copyfd, &statbuf,
5937+
status = (smart_rename (tmpname, input_filename,
59585938
preserve_dates) != 0);
59595939
}
59605940
else
5961-
{
5962-
#if !defined (_WIN32) || defined (__CYGWIN32__)
5963-
if (copyfd >= 0)
5964-
close (copyfd);
5965-
#endif
5966-
unlink_if_ordinary (tmpname);
5967-
}
5941+
unlink_if_ordinary (tmpname);
59685942

59695943
if (tmpname != output_filename)
59705944
free (tmpname);

binutils/rename.c

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -131,55 +131,17 @@ set_times (const char *destination, const struct stat *statbuf)
131131
#endif
132132
#endif
133133

134-
#if !defined (_WIN32) || defined (__CYGWIN32__)
135-
/* Try to preserve the permission bits and ownership of an existing file when
136-
rename overwrites it. FD is the file being renamed and TARGET_STAT has the
137-
status of the file that was overwritten. */
138-
static void
139-
try_preserve_permissions (int fd, struct stat *target_stat)
140-
{
141-
struct stat from_stat;
142-
int ret = 0;
143-
144-
if (fstat (fd, &from_stat) != 0)
145-
return;
146-
147-
int from_mode = from_stat.st_mode & 0777;
148-
int to_mode = target_stat->st_mode & 0777;
149-
150-
/* Fix up permissions before we potentially lose ownership with fchown.
151-
Clear the setxid bits because in case the fchown below fails then we don't
152-
want to end up with a sxid file owned by the invoking user. If the user
153-
hasn't changed or if fchown succeeded, we add back the sxid bits at the
154-
end. */
155-
if (from_mode != to_mode)
156-
fchmod (fd, to_mode);
157-
158-
/* Fix up ownership, this will clear the setxid bits. */
159-
if (from_stat.st_uid != target_stat->st_uid
160-
|| from_stat.st_gid != target_stat->st_gid)
161-
ret = fchown (fd, target_stat->st_uid, target_stat->st_gid);
162-
163-
/* Fix up the sxid bits if either the fchown wasn't needed or it
164-
succeeded. */
165-
if (ret == 0)
166-
fchmod (fd, target_stat->st_mode & 07777);
167-
}
168-
#endif
169-
170-
/* Rename FROM to TO, copying if TO is either a link or is not a regular file.
171-
FD is an open file descriptor pointing to FROM that we can use to safely fix
172-
up permissions of the file after renaming. TARGET_STAT has the file status
173-
that is used to fix up permissions and timestamps after rename. Return 0 if
174-
ok, -1 if error and FD is closed before returning. */
134+
/* Rename FROM to TO, copying if TO is a link.
135+
Return 0 if ok, -1 if error. */
175136

176137
int
177-
smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
178-
struct stat *target_stat ATTRIBUTE_UNUSED,
179-
int preserve_dates ATTRIBUTE_UNUSED)
138+
smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
180139
{
140+
bfd_boolean exists;
141+
struct stat s;
181142
int ret = 0;
182-
bfd_boolean exists = target_stat != NULL;
143+
144+
exists = lstat (to, &s) == 0;
183145

184146
#if defined (_WIN32) && !defined (__CYGWIN32__)
185147
/* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
@@ -196,35 +158,36 @@ smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
196158
unlink (from);
197159
}
198160
#else
199-
/* Avoid a full copy and use rename if we can fix up permissions of the
200-
file after renaming, i.e.:
201-
202-
- TO is not a symbolic link
203-
- TO is a regular file with only one hard link
204-
- We have permission to write to TO
205-
- FD is available to safely fix up permissions to be the same as the file
206-
we overwrote with the rename.
207-
208-
Note though that the actual file on disk that TARGET_STAT describes may
209-
have changed and we're only trying to preserve the status we know about.
210-
At no point do we try to interact with the new file changes, so there can
211-
only be two outcomes, i.e. either the external file change survives
212-
without knowledge of our change (if it happens after the rename syscall)
213-
or our rename and permissions fixup survive without any knowledge of the
214-
external change. */
161+
/* Use rename only if TO is not a symbolic link and has
162+
only one hard link, and we have permission to write to it. */
215163
if (! exists
216-
|| (fd >= 0
217-
&& !S_ISLNK (target_stat->st_mode)
218-
&& S_ISREG (target_stat->st_mode)
219-
&& (target_stat->st_mode & S_IWUSR)
220-
&& target_stat->st_nlink == 1)
164+
|| (!S_ISLNK (s.st_mode)
165+
&& S_ISREG (s.st_mode)
166+
&& (s.st_mode & S_IWUSR)
167+
&& s.st_nlink == 1)
221168
)
222169
{
223170
ret = rename (from, to);
224171
if (ret == 0)
225172
{
226173
if (exists)
227-
try_preserve_permissions (fd, target_stat);
174+
{
175+
/* Try to preserve the permission bits and ownership of
176+
TO. First get the mode right except for the setuid
177+
bit. Then change the ownership. Then fix the setuid
178+
bit. We do the chmod before the chown because if the
179+
chown succeeds, and we are a normal user, we won't be
180+
able to do the chmod afterward. We don't bother to
181+
fix the setuid bit first because that might introduce
182+
a fleeting security problem, and because the chown
183+
will clear the setuid bit anyhow. We only fix the
184+
setuid bit if the chown succeeds, because we don't
185+
want to introduce an unexpected setuid file owned by
186+
the user running objcopy. */
187+
chmod (to, s.st_mode & 0777);
188+
if (chown (to, s.st_uid, s.st_gid) >= 0)
189+
chmod (to, s.st_mode & 07777);
190+
}
228191
}
229192
else
230193
{
@@ -240,11 +203,9 @@ smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
240203
non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
241204

242205
if (preserve_dates)
243-
set_times (to, target_stat);
206+
set_times (to, &s);
244207
unlink (from);
245208
}
246-
if (fd >= 0)
247-
close (fd);
248209
#endif /* _WIN32 && !__CYGWIN32__ */
249210

250211
return ret;

0 commit comments

Comments
 (0)