with Bash's source
it is possible to execute a script without execution bit set. This is documented and expected behavoir, but isn't this against the use of an execution bit?
I know, that source
doesn't create a subshell.
with Bash's I know, that |
||||
Bash is an interpreter; it accepts input and does whatever it wants to. It doesn't need to heed the executable bit. In fact, Bash is portable, and can run on operating systems and filesystems that don't have any concept of an executable bit. What does care about the executable bit is the operating system kernel. When the Linux kernel performs an Note that the executable bit is a rather discretionary kind of control. On a Linux x86-64 system, for example, you can bypass the kernel's verification of the executable bit by explicitly invoking
This is somewhat analogous to sourcing Bash source code in Bash, except that |
|||||||||||||||||||||
|
There's nothing against the use of execution bit, because the shell only need read permission to read the content of file. The execution bit is only required when you run the script. Here the shell will |
|||||||||||||||||||||
|
The executable bit (unlike the rest) on nonsetuid and nonsetguid files isn't much of a security mechanism. Anything you can read, you can run indirectly, and Linux will let you indirectly read anything you can run but not directly read (that should be enough to punch a hole in the concept of non-set(g)uid x-bit being a security measure). It's more of a convenience thing: Let the system run it for me directly if the the bit is set, otherwise I need to do it indirectly ( You can set it for convenience if you intend to both in-source and execute your insourcable. Apparently, however, many shared library implementers share your thinking and consequently, many systems do require that shared libraries, which are essentially the native equivalent of shell insourcables, be marked executable in order to be usable. See Why are shared libraries executable?. |
|||||||||||||||||||||
|
That's a good question! Unix uses the executable bit to distinguish between programs and data. The OS does not require the execution bit, since a sourced script is not passed to the OS for execution as a new process. But the shell treats a sourced script as a program, and will look in The question must have come up long ago. The design of the Bourne shell was the result of "a long sequence of modification, dialog, discussion" among the denizens of Bell Labs, and many design decisions were discussed by S.R. Bourne and others over the years. Unfortunately, my quick look didn't find any discussion of the source feature (in my defense, it's hard to google for it). What I did find is that the "." command does not appear in this early introduction to the shell by Bourne himself, but it is present in the more mature Version 7 version. Absent authority, here's my own interpretation:
|
|||||||||||||
|
As far as the OS is concerned, a file containing shell script is just data. If you pass the name of such a data file to the How would the execute bit be at all relevant in that case? |
|||
|
The distinction is important because you may have a file of shell commands which is not useful as an executable, but only useful when sourced. For this file you can turn off the execute bit and then it will never be accessed unless explicitly in a source command. The reason for such a thing is to have side effects on the shell it is run from. For a specific example, I have a script called fix_path which looks at and modifies the path. |
|||
|
Just in case anyone is interested in further studies and/or clarification: In a nearly POSIX compliant shell implemented a while ago the internal workings of the 'exec_program()' and the 'builtin_source()' functions are very exemplary. In those functions you see exactly what is the difference between them: https://github.com/rsenn/shish/blob/master/src/builtin/builtin_source.c https://github.com/rsenn/shish/blob/master/src/exec/exec_program.c basically sourcing can be seen as the shell temporarily redirecting its internal file descriptor where it parses shell script from (the terminal in interactive mode). so it is very similar to other redirections like so executing is handled by the I recall seeing some systems which allow execution of ELF/a.out binaries, but via executing the "/lib/ld-dynamic-linker.so" and with the binary program (without exec bit) as first argument. I believe that was on some DEC Alpha or VAX machines (Could it have been SCO Unix?) |
|||
|
Another point of view: Sourced script basically consists of shell builtins and program calls. Shell builtins (with So it is not against the use of an execution bit, because nothing without execution bit will run. The validation doesn't occur for the sourced script as a whole; it is performed for every part separately, but it is. |
|||||||||
|
chmod
can let you set permissions (including `x) with an octal number gives some clue to what era it comes from. I wouldn't be surprised if it started as a quick and dirty "this is a binary file you can execute" indicator, from the days before she-bang was invented, but I have no evidence to that – infixed Jun 22 at 17:49cp /sbin/suidexecutable /tmp/mycopy; /tmp/mycopy
– infixed Jun 27 at 12:51