From 242003f6553e03889f917462948dbb1a9fd83342 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sun, 24 Dec 2006 17:08:04 +0000 Subject: [PATCH] Work around an fchownat bug in glibc-2.4: http://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html This bug would cause "chown -RP ... DIR" to follow symlinks in DIR, in spite of the -P option. * m4/openat.m4 (gl_FUNC_FCHOWNAT, gl_FUNC_FCHOWNAT_DEREF_BUG): New macros. (gl_PREREQ_OPENAT): Require gl_FUNC_FCHOWNAT. * modules/openat (Files): Add lib/fchownat.c. * lib/openat.c (fchownat): Don't define here. Move to... * lib/fchownat.c: ...this new file. --- ChangeLog | 13 ++++++++++++ lib/fchownat.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ lib/openat.c | 16 --------------- m4/openat.m4 | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- modules/openat | 1 + 5 files changed, 125 insertions(+), 17 deletions(-) create mode 100644 lib/fchownat.c diff --git a/ChangeLog b/ChangeLog index 23fe11031..64d321464 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2006-12-24 Jim Meyering + + Work around an fchownat bug in glibc-2.4: + http://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html + This bug would cause "chown -RP ... DIR" to follow symlinks in DIR, + in spite of the -P option. + * m4/openat.m4 (gl_FUNC_FCHOWNAT, gl_FUNC_FCHOWNAT_DEREF_BUG): + New macros. + (gl_PREREQ_OPENAT): Require gl_FUNC_FCHOWNAT. + * modules/openat (Files): Add lib/fchownat.c. + * lib/openat.c (fchownat): Don't define here. Move to... + * lib/fchownat.c: ...this new file. + 2006-12-23 Paul Eggert Fix bug reported by Bruno Haible in diff --git a/lib/fchownat.c b/lib/fchownat.c new file mode 100644 index 000000000..382ed7bc1 --- /dev/null +++ b/lib/fchownat.c @@ -0,0 +1,49 @@ +/* This function serves as replacement for a missing fchownat function, + as well as a work around for the fchownat bug in glibc-2.4: + + when the buggy fchownat-with-AT_SYMLINK_NOFOLLOW operates on a symlink, it + mistakenly affects the symlink referent, rather than the symlink itself. + + Copyright (C) 2006 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* written by Jim Meyering */ + +#include + +#include "openat.h" + +#include + +#include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */ +#include "save-cwd.h" +#include "openat-priv.h" + +/* Replacement for Solaris' function by the same name. + Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the + directory open on descriptor FD. If FLAG is AT_SYMLINK_NOFOLLOW, then + use lchown, otherwise, use chown. If possible, do it without changing + the working directory. Otherwise, resort to using save_cwd/fchdir, + then mkdir/restore_cwd. If either the save_cwd or the restore_cwd + fails, then give a diagnostic and exit nonzero. */ + +#define AT_FUNC_NAME fchownat +#define AT_FUNC_F1 lchown +#define AT_FUNC_F2 chown +#define AT_FUNC_USE_F1_COND flag == AT_SYMLINK_NOFOLLOW +#define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group, int flag +#define AT_FUNC_POST_FILE_ARGS , owner, group +#include "at-func.c" diff --git a/lib/openat.c b/lib/openat.c index 7f92c7cc7..cd496540e 100644 --- a/lib/openat.c +++ b/lib/openat.c @@ -268,19 +268,3 @@ fdopendir (int fd) #undef AT_FUNC_USE_F1_COND #undef AT_FUNC_POST_FILE_PARAM_DECLS #undef AT_FUNC_POST_FILE_ARGS - -/* Replacement for Solaris' function by the same name. - Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the - directory open on descriptor FD. If FLAG is AT_SYMLINK_NOFOLLOW, then - use lchown, otherwise, use chown. If possible, do it without changing - the working directory. Otherwise, resort to using save_cwd/fchdir, - then mkdir/restore_cwd. If either the save_cwd or the restore_cwd - fails, then give a diagnostic and exit nonzero. */ - -#define AT_FUNC_NAME fchownat -#define AT_FUNC_F1 lchown -#define AT_FUNC_F2 chown -#define AT_FUNC_USE_F1_COND flag == AT_SYMLINK_NOFOLLOW -#define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group, int flag -#define AT_FUNC_POST_FILE_ARGS , owner, group -#include "at-func.c" diff --git a/m4/openat.m4 b/m4/openat.m4 index e7286487e..88efc7569 100644 --- a/m4/openat.m4 +++ b/m4/openat.m4 @@ -1,4 +1,4 @@ -#serial 12 +#serial 13 # See if we need to use our replacement for Solaris' openat et al functions. dnl Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. @@ -24,6 +24,67 @@ AC_DEFUN([gl_FUNC_OPENAT], [Define to rpl_ if the openat replacement function should be used.]) gl_PREREQ_OPENAT;; esac + AC_REQUIRE([gl_FUNC_FCHOWNAT]) +]) + +# gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]]) +AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG], +[ + AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW], + gl_cv_func_fchownat_nofollow_works, + [ + gl_dangle=conftest.dangle + # Remove any remnants of a previous test. + rm -f $gl_dangle + # Arrange for deletion of the temporary file this test creates. + ac_clean_files="$ac_clean_files $gl_dangle" + AC_RUN_IFELSE( + [AC_LANG_SOURCE( + [[ +#include +#include +#include +#include +#include +int +main () +{ + return (fchownat (AT_FDCWD, "$gl_dangle", -1, getgid (), + AT_SYMLINK_NOFOLLOW) != 0 + && errno == ENOENT); +} + ]])], + [gl_cv_func_fchownat_nofollow_works=yes], + [gl_cv_func_fchownat_nofollow_works=no], + [gl_cv_func_fchownat_nofollow_works=no], + ) + ]) + AS_IF([test $gl_cv_func_fchownat_nofollow_works = no], [$1], [$2]) +]) + +# If we have the fchownat function, and it has the bug (in glibc-2.4) +# that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then +# use the replacement function. +# Also use the replacement function if fchownat is simply not available. +AC_DEFUN([gl_FUNC_FCHOWNAT], +[ + # Assume we'll use the replacement function. + # The only case in which we won't is when we have fchownat, and it works. + use_replacement_fchownat=yes + + AC_CHECK_FUNC([fchownat], [have_fchownat=yes], [have_fchownat=no]) + if test $have_fchownat = yes; then + gl_FUNC_FCHOWNAT_DEREF_BUG([have_fchownat_bug=yes]) + if test $have_fchownat_bug = no; then + use_replacement_fchownat=no + fi + fi + + if test $use_replacement_fchownat = yes; then + AC_LIBOBJ(fchownat) + AC_DEFINE(fchownat, rpl_fchownat, + [Define to rpl_fchownat if the replacement function should be used.]) + fi ]) AC_DEFUN([gl_PREREQ_OPENAT], diff --git a/modules/openat b/modules/openat index 7754a3d7d..6f2266fd2 100644 --- a/modules/openat +++ b/modules/openat @@ -4,6 +4,7 @@ Open a file at a directory. Files: lib/at-func.c lib/fchmodat.c +lib/fchownat.c lib/fstatat.c lib/mkdirat.c lib/openat.c -- 2.11.0