bug-bash
[Top][All Lists]
Advanced

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

Re: bug in [ -f file ] test


From: László Házy
Subject: Re: bug in [ -f file ] test
Date: Thu, 28 Jul 2016 12:52:42 -0400

Thanks for the effort Chet. Here are the results of some tests I have done, including what you requested. Note that I have SELinux as disabled.

[root]$ getenforce
Disabled


[user1]# cat /home/user1/file
This is file.

[user2]# cat /home/user1/file
This is file.



[user1]# cat /var/tmp/link
cat: /var/tmp/link: Permission denied

[user2]$ cat /var/tmp/link
This is file.



[user1]$ stat /home/user1/file
  File: ‘/home/user1/file’
  Size: 14              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 3414083     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  501/  user1)   Gid: (  100/   users)
Access: 2016-07-28 12:08:34.186872745 -0400
Modify: 2016-07-28 12:08:30.879845375 -0400
Change: 2016-07-28 12:08:30.950845962 -0400
 Birth: -

[user2]# stat /home/user1/file
  File: ‘/home/user1/file’
  Size: 14              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 3414083     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  501/  user1)   Gid: (  100/   users)
Access: 2016-07-28 12:08:34.186872745 -0400
Modify: 2016-07-28 12:08:30.879845375 -0400
Change: 2016-07-28 12:08:30.950845962 -0400
 Birth: -


 
[user1]# stat /var/tmp/link
  File: ‘/var/tmp/link’ -> ‘/home/user1/file’
  Size: 17              Blocks: 0          IO Block: 4096   symbolic link
Device: 808h/2056d      Inode: 131183      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (  500/  user2)   Gid: (  100/   users)
Access: 2016-07-27 12:17:07.899994243 -0400
Modify: 2016-07-27 12:17:07.398991028 -0400
Change: 2016-07-27 12:17:07.398991028 -0400
 Birth: -

[user2]# stat /var/tmp/link
  File: ‘/var/tmp/link’ -> ‘/home/user1/file’
  Size: 17              Blocks: 0          IO Block: 4096   symbolic link
Device: 808h/2056d      Inode: 131183      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (  500/  user2)   Gid: (  100/   users)
Access: 2016-07-27 12:17:07.899994243 -0400
Modify: 2016-07-27 12:17:07.398991028 -0400
Change: 2016-07-27 12:17:07.398991028 -0400
 Birth: -


 
Then, I created the /home/user1/test.sh script with the following content:
#!/bin/bash

[[ -f /var/tmp/link ]]
echo $?
 
End of /home/user1/test.sh.
 
 
 
[user1]$ strace ./test.sh
execve("./test.sh", ["./test.sh"], [/* 59 vars */]) = 0
brk(NULL)                               = 0x80d51000
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb77b5000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=128315, ...}) = 0
mmap2(NULL, 128315, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7795000
close(3)                                = 0
open("/lib/libtinfo.so.5", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\220^\0\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=142888, ...}) = 0
mmap2(NULL, 141008, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7772000
mmap2(0xb7791000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f000) = 0xb7791000
close(3)                                = 0
open("/lib/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\220\n\0\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=18272, ...}) = 0
mmap2(NULL, 16508, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb776d000
mmap2(0xb7770000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0xb7770000
close(3)                                = 0
open("/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\3\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\260\206\1\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=2125576, ...}) = 0
mmap2(NULL, 1886780, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb75a0000
mmap2(0xb7767000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c7000) = 0xb7767000
mmap2(0xb776a000, 10812, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb776a000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb759f000
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb759e000
set_thread_area({entry_number:-1, base_addr:0xb759e700, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0 (entry_number:6)
mprotect(0xb7767000, 8192, PROT_READ)   = 0
mprotect(0xb7770000, 4096, PROT_READ)   = 0
mprotect(0xb7791000, 12288, PROT_READ)  = 0
mprotect(0x801be000, 8192, PROT_READ)   = 0
mprotect(0xb77dc000, 4096, PROT_READ)   = 0
munmap(0xb7795000, 128315)              = 0
open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
close(3)                                = 0
brk(NULL)                               = 0x80d51000
brk(0x80d72000)                         = 0x80d72000
brk(NULL)                               = 0x80d72000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=111950656, ...}) = 0
mmap2(NULL, 2097152, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb739e000
mmap2(NULL, 1253376, PROT_READ, MAP_PRIVATE, 3, 0x1079000) = 0xb726c000
close(3)                                = 0
getuid32()                              = 501
getgid32()                              = 100
geteuid32()                             = 501
getegid32()                             = 100
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
brk(NULL)                               = 0x80d72000
open("/proc/meminfo", O_RDONLY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb77b4000
read(3, "MemTotal:        8239856 kB\nMemF"..., 1024) = 1024
close(3)                                = 0
munmap(0xb77b4000, 4096)                = 0
rt_sigaction(SIGCHLD, {SIG_DFL, [], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGCHLD, {SIG_DFL, [], SA_RESTART}, {SIG_DFL, [], SA_RESTART}, 8) = 0
rt_sigaction(SIGINT, {SIG_DFL, [], 0}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGINT, {SIG_DFL, [], 0}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_DFL, [], 0}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_DFL, [], 0}, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
rt_sigaction(SIGQUIT, {SIG_IGN, [], 0}, {SIG_DFL, [], 0}, 8) = 0
uname({sysname="Linux", nodename="ws021", ...}) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=26252, ...}) = 0
mmap2(NULL, 26252, PROT_READ, MAP_SHARED, 3, 0) = 0xb77ae000
close(3)                                = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
stat64("/home/user1", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat64(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
getpid()                                = 8430
getppid()                               = 8428
getpgrp()                               = 8428
rt_sigaction(SIGCHLD, {0x80107930, [], SA_RESTART}, {SIG_DFL, [], SA_RESTART}, 8) = 0
ugetrlimit(RLIMIT_NPROC, {rlim_cur=64221, rlim_max=64221}) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
open("./test.sh", O_RDONLY|O_LARGEFILE) = 3
ioctl(3, TCGETS, 0xbfaeb1ec)            = -1 ENOTTY (Inappropriate ioctl for device)
_llseek(3, 0, [0], SEEK_CUR)            = 0
read(3, "#!/bin/bash\n\n[[ -f /var/tmp/link"..., 80) = 45
_llseek(3, 0, [0], SEEK_SET)            = 0
ugetrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=4*1024}) = 0
fcntl64(255, F_GETFD)                   = -1 EBADF (Bad file descriptor)
dup2(3, 255)                            = 255
close(3)                                = 0
fcntl64(255, F_SETFD, FD_CLOEXEC)       = 0
fcntl64(255, F_GETFL)                   = 0x8000 (flags O_RDONLY|O_LARGEFILE)
fstat64(255, {st_mode=S_IFREG|0755, st_size=45, ...}) = 0
_llseek(255, 0, [0], SEEK_CUR)          = 0
read(255, "#!/bin/bash\n\n[[ -f /var/tmp/link"..., 45) = 45
stat64("/var/tmp/link", 0xbfaeb038)     = -1 EACCES (Permission denied)
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb77ad000
write(1, "1\n", 21
)                      = 2
read(255, "", 45)                       = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
exit_group(0)                           = ?
+++ exited with 0 +++


[user2]# cd /home/user1
[user2]# strace ./test.sh
execve("./test.sh", ["./test.sh"], [/* 58 vars */]) = 0
brk(NULL)                               = 0x80d1c000
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7778000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=128315, ...}) = 0
mmap2(NULL, 128315, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7758000
close(3)                                = 0
open("/lib/libtinfo.so.5", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\220^\0\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=142888, ...}) = 0
mmap2(NULL, 141008, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7735000
mmap2(0xb7754000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f000) = 0xb7754000
close(3)                                = 0
open("/lib/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\220\n\0\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=18272, ...}) = 0
mmap2(NULL, 16508, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7730000
mmap2(0xb7733000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0xb7733000
close(3)                                = 0
open("/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\3\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\260\206\1\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=2125576, ...}) = 0
mmap2(NULL, 1886780, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7563000
mmap2(0xb772a000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c7000) = 0xb772a000
mmap2(0xb772d000, 10812, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb772d000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7562000
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7561000
set_thread_area({entry_number:-1, base_addr:0xb7561700, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0 (entry_number:6)
mprotect(0xb772a000, 8192, PROT_READ)   = 0
mprotect(0xb7733000, 4096, PROT_READ)   = 0
mprotect(0xb7754000, 12288, PROT_READ)  = 0
mprotect(0x801ba000, 8192, PROT_READ)   = 0
mprotect(0xb779f000, 4096, PROT_READ)   = 0
munmap(0xb7758000, 128315)              = 0
open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
close(3)                                = 0
brk(NULL)                               = 0x80d1c000
brk(0x80d3d000)                         = 0x80d3d000
brk(NULL)                               = 0x80d3d000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=111950656, ...}) = 0
mmap2(NULL, 2097152, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7361000
mmap2(NULL, 1253376, PROT_READ, MAP_PRIVATE, 3, 0x1079000) = 0xb722f000
close(3)                                = 0
getuid32()                              = 500
getgid32()                              = 100
geteuid32()                             = 500
getegid32()                             = 100
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
brk(NULL)                               = 0x80d3d000
open("/proc/meminfo", O_RDONLY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7777000
read(3, "MemTotal:        8239856 kB\nMemF"..., 1024) = 1024
close(3)                                = 0
munmap(0xb7777000, 4096)                = 0
rt_sigaction(SIGCHLD, {SIG_DFL, [], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGCHLD, {SIG_DFL, [], SA_RESTART}, {SIG_DFL, [], SA_RESTART}, 8) = 0
rt_sigaction(SIGINT, {SIG_DFL, [], 0}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGINT, {SIG_DFL, [], 0}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_DFL, [], 0}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_DFL, [], 0}, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
rt_sigaction(SIGQUIT, {SIG_IGN, [], 0}, {SIG_DFL, [], 0}, 8) = 0
uname({sysname="Linux", nodename="ws021", ...}) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=26252, ...}) = 0
mmap2(NULL, 26252, PROT_READ, MAP_SHARED, 3, 0) = 0xb7771000
close(3)                                = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
stat64("/home/user1", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat64(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
getpid()                                = 8451
getppid()                               = 8449
getpgrp()                               = 8449
rt_sigaction(SIGCHLD, {0x80103930, [], SA_RESTART}, {SIG_DFL, [], SA_RESTART}, 8) = 0
ugetrlimit(RLIMIT_NPROC, {rlim_cur=64221, rlim_max=64221}) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
open("./test.sh", O_RDONLY|O_LARGEFILE) = 3
ioctl(3, TCGETS, 0xbff880dc)            = -1 ENOTTY (Inappropriate ioctl for device)
_llseek(3, 0, [0], SEEK_CUR)            = 0
read(3, "#!/bin/bash\n\n[[ -f /var/tmp/link"..., 80) = 45
_llseek(3, 0, [0], SEEK_SET)            = 0
ugetrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=4*1024}) = 0
fcntl64(255, F_GETFD)                   = -1 EBADF (Bad file descriptor)
dup2(3, 255)                            = 255
close(3)                                = 0
fcntl64(255, F_SETFD, FD_CLOEXEC)       = 0
fcntl64(255, F_GETFL)                   = 0x8000 (flags O_RDONLY|O_LARGEFILE)
fstat64(255, {st_mode=S_IFREG|0755, st_size=45, ...}) = 0
_llseek(255, 0, [0], SEEK_CUR)          = 0
read(255, "#!/bin/bash\n\n[[ -f /var/tmp/link"..., 45) = 45
stat64("/var/tmp/link", {st_mode=S_IFREG|0644, st_size=14, ...}) = 0
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 3), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7770000
write(1, "0\n", 20
)                      = 2
read(255, "", 45)                       = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
exit_group(0)                           = ?
+++ exited with 0 +++







On Wed, 2016-07-27 at 18:33 -0400, Chet Ramey wrote:
On 7/27/16 3:34 PM, László Házy wrote:
You have probably not done the first command: "[user1]$ chmod g+rx /home/user1". In my case, there is no access problem. I can ls and cd. Thing is, even root gets the wrong answer if it does the "is file?" query.
I performed that command, but I tore it all down and rebuilt it anyway. Running a system call tracer (strace) shows that stat ("/var/tmp/link", ) returns -1 with errno == EACCES. `cat' displays "Permission Denied", and `cp' says "cannot stat". What does strace show when you run it as user1 with a script that contains [[ -f /var/tmp/link ]] echo $? ?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]