[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Linux permissions and which(1)



I was looking at the source code to the which(1) command (apt-get source 
which = which-2.14 ).

As you might imagine, which(1) prepends the path to a name, checks if 
it exists and then checks if it's executable by the current process 
(your uid).

In the which package is a file "bash.c" that contains the following 
code.  At this point the file has been found and now it's just checking 
permissions.

  /* If we are the owner of the file, the owner execute bit applies. */
  if (user_id == finfo.st_uid && X_BIT (u_mode_bits (finfo.st_mode)))
    return (FS_EXISTS | FS_EXECABLE);

  /* If we are in the owning group, the group permissions apply. */
  if (group_member (finfo.st_gid) && X_BIT (g_mode_bits (finfo.st_mode)))
    return (FS_EXISTS | FS_EXECABLE);

  /* If `others' have execute permission to the file, then so do we,
     since we are also `others'. */
  if (X_BIT (o_mode_bits (finfo.st_mode)))
    return (FS_EXISTS | FS_EXECABLE);
  else
    return (FS_EXISTS);

This seems wrong, if I understand Linux permissions correctly.  If you 
are the *owner* of a file, then it's the *owner* permissions that count.  
If the owner perms say you can't read it then it doesn't matter what the 
group and other perms are.

In that code above if you are the owner but don't have execute perms 
then it moves onto checking the group perms, and then "other" perms.

Am I crazy is is that code wrong?


moseley@bumby:~$ cat t.c
#include <stdio.h>
int main( void )
{
   printf("hello world\n"); 
   return 0;
}

moseley@bumby:~$ gcc -o t t.c

moseley@bumby:~$ chmod 755 t

moseley@bumby:~$ ./t
hello world

moseley@bumby:~$ chmod 655 t

moseley@bumby:~$ ./t
bash: ./t: Permission denied

moseley@bumby:~$ ls -l t
-rw-r-xr-x    1 moseley  moseley      4161 2003-09-04 21:42 t



-- 
Bill Moseley
moseley@hank.org



Reply to: