Execute system calls directly

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












Provided a user is authorized to access something, how can he execute a system call directly, like geteuid() - get effective user ID (it's just an example) from bash, how could I do it?







share|improve this question


















  • 2




    Invoking a system call directly involves loading one or more registers with values and executing an assembly instruction. You're not going to be able to do that directly from bash.
    – Andy Dalton
    Nov 6 '17 at 14:17






  • 1




    The most straight-forward way would be to write a small C program. Are you asking for a way to do this in pure Bash?
    – igal
    Nov 6 '17 at 14:31






  • 1




    1. Why not use the library call wrapper - geteuid() - to the syscall - sys_geteuid() - in Linux? 2. Why do you want to use a syscall directly?
    – schaiba
    Nov 6 '17 at 14:33










  • I do wonder what the purpose behind this is? It doesn't make much sense to manually make the system calls e.g. for copying a file, since you could use cp or cat directly. (And that's not even very hard to do on the system call level.) Is there some specific operation or system call you have in mind, or is this just about how system calls are made in general, or something else?
    – ilkkachu
    Nov 6 '17 at 14:47






  • 3




    Why do you ask? Are you looking just for id command, or are you wanting to add a new system call into your kernel and asking how you would use it? Is the question specific to geteuid or generic to all system calls of syscalls(2)? Please edit your question to improve and motivate it!
    – Basile Starynkevitch
    Nov 6 '17 at 14:51















up vote
1
down vote

favorite
1












Provided a user is authorized to access something, how can he execute a system call directly, like geteuid() - get effective user ID (it's just an example) from bash, how could I do it?







share|improve this question


















  • 2




    Invoking a system call directly involves loading one or more registers with values and executing an assembly instruction. You're not going to be able to do that directly from bash.
    – Andy Dalton
    Nov 6 '17 at 14:17






  • 1




    The most straight-forward way would be to write a small C program. Are you asking for a way to do this in pure Bash?
    – igal
    Nov 6 '17 at 14:31






  • 1




    1. Why not use the library call wrapper - geteuid() - to the syscall - sys_geteuid() - in Linux? 2. Why do you want to use a syscall directly?
    – schaiba
    Nov 6 '17 at 14:33










  • I do wonder what the purpose behind this is? It doesn't make much sense to manually make the system calls e.g. for copying a file, since you could use cp or cat directly. (And that's not even very hard to do on the system call level.) Is there some specific operation or system call you have in mind, or is this just about how system calls are made in general, or something else?
    – ilkkachu
    Nov 6 '17 at 14:47






  • 3




    Why do you ask? Are you looking just for id command, or are you wanting to add a new system call into your kernel and asking how you would use it? Is the question specific to geteuid or generic to all system calls of syscalls(2)? Please edit your question to improve and motivate it!
    – Basile Starynkevitch
    Nov 6 '17 at 14:51













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





Provided a user is authorized to access something, how can he execute a system call directly, like geteuid() - get effective user ID (it's just an example) from bash, how could I do it?







share|improve this question














Provided a user is authorized to access something, how can he execute a system call directly, like geteuid() - get effective user ID (it's just an example) from bash, how could I do it?









share|improve this question













share|improve this question




share|improve this question








edited Nov 6 '17 at 18:41

























asked Nov 6 '17 at 14:04









Pierre B

5332522




5332522







  • 2




    Invoking a system call directly involves loading one or more registers with values and executing an assembly instruction. You're not going to be able to do that directly from bash.
    – Andy Dalton
    Nov 6 '17 at 14:17






  • 1




    The most straight-forward way would be to write a small C program. Are you asking for a way to do this in pure Bash?
    – igal
    Nov 6 '17 at 14:31






  • 1




    1. Why not use the library call wrapper - geteuid() - to the syscall - sys_geteuid() - in Linux? 2. Why do you want to use a syscall directly?
    – schaiba
    Nov 6 '17 at 14:33










  • I do wonder what the purpose behind this is? It doesn't make much sense to manually make the system calls e.g. for copying a file, since you could use cp or cat directly. (And that's not even very hard to do on the system call level.) Is there some specific operation or system call you have in mind, or is this just about how system calls are made in general, or something else?
    – ilkkachu
    Nov 6 '17 at 14:47






  • 3




    Why do you ask? Are you looking just for id command, or are you wanting to add a new system call into your kernel and asking how you would use it? Is the question specific to geteuid or generic to all system calls of syscalls(2)? Please edit your question to improve and motivate it!
    – Basile Starynkevitch
    Nov 6 '17 at 14:51













  • 2




    Invoking a system call directly involves loading one or more registers with values and executing an assembly instruction. You're not going to be able to do that directly from bash.
    – Andy Dalton
    Nov 6 '17 at 14:17






  • 1




    The most straight-forward way would be to write a small C program. Are you asking for a way to do this in pure Bash?
    – igal
    Nov 6 '17 at 14:31






  • 1




    1. Why not use the library call wrapper - geteuid() - to the syscall - sys_geteuid() - in Linux? 2. Why do you want to use a syscall directly?
    – schaiba
    Nov 6 '17 at 14:33










  • I do wonder what the purpose behind this is? It doesn't make much sense to manually make the system calls e.g. for copying a file, since you could use cp or cat directly. (And that's not even very hard to do on the system call level.) Is there some specific operation or system call you have in mind, or is this just about how system calls are made in general, or something else?
    – ilkkachu
    Nov 6 '17 at 14:47






  • 3




    Why do you ask? Are you looking just for id command, or are you wanting to add a new system call into your kernel and asking how you would use it? Is the question specific to geteuid or generic to all system calls of syscalls(2)? Please edit your question to improve and motivate it!
    – Basile Starynkevitch
    Nov 6 '17 at 14:51








2




2




Invoking a system call directly involves loading one or more registers with values and executing an assembly instruction. You're not going to be able to do that directly from bash.
– Andy Dalton
Nov 6 '17 at 14:17




Invoking a system call directly involves loading one or more registers with values and executing an assembly instruction. You're not going to be able to do that directly from bash.
– Andy Dalton
Nov 6 '17 at 14:17




1




1




The most straight-forward way would be to write a small C program. Are you asking for a way to do this in pure Bash?
– igal
Nov 6 '17 at 14:31




The most straight-forward way would be to write a small C program. Are you asking for a way to do this in pure Bash?
– igal
Nov 6 '17 at 14:31




1




1




1. Why not use the library call wrapper - geteuid() - to the syscall - sys_geteuid() - in Linux? 2. Why do you want to use a syscall directly?
– schaiba
Nov 6 '17 at 14:33




1. Why not use the library call wrapper - geteuid() - to the syscall - sys_geteuid() - in Linux? 2. Why do you want to use a syscall directly?
– schaiba
Nov 6 '17 at 14:33












I do wonder what the purpose behind this is? It doesn't make much sense to manually make the system calls e.g. for copying a file, since you could use cp or cat directly. (And that's not even very hard to do on the system call level.) Is there some specific operation or system call you have in mind, or is this just about how system calls are made in general, or something else?
– ilkkachu
Nov 6 '17 at 14:47




I do wonder what the purpose behind this is? It doesn't make much sense to manually make the system calls e.g. for copying a file, since you could use cp or cat directly. (And that's not even very hard to do on the system call level.) Is there some specific operation or system call you have in mind, or is this just about how system calls are made in general, or something else?
– ilkkachu
Nov 6 '17 at 14:47




3




3




Why do you ask? Are you looking just for id command, or are you wanting to add a new system call into your kernel and asking how you would use it? Is the question specific to geteuid or generic to all system calls of syscalls(2)? Please edit your question to improve and motivate it!
– Basile Starynkevitch
Nov 6 '17 at 14:51





Why do you ask? Are you looking just for id command, or are you wanting to add a new system call into your kernel and asking how you would use it? Is the question specific to geteuid or generic to all system calls of syscalls(2)? Please edit your question to improve and motivate it!
– Basile Starynkevitch
Nov 6 '17 at 14:51











2 Answers
2






active

oldest

votes

















up vote
9
down vote













User-space kernel-space communication via system calls is done in terms of memory locations and machine registers. That's way below the abstraction level of shells, which operate mainly with text strings.



That said, in bash, you can use the https://github.com/taviso/ctypes.sh plugin to get through the text-string abstraction down to C-level granularity:



$ . ctypes.sh
$ dlcall -r long geteuid
long:1001


For this particular operation though, it would be much simpler, more idiomatic, and more efficient to simply use bash's magic $UID variable.



$ echo "$EUID" #effectively a cached geteuid call
1001





share|improve this answer





























    up vote
    5
    down vote













    To get the uid, write your own C program (or some shell plugin, if your shell accepts them; FYI zsh can have plugins, called modules.) or more simply run the id(1) command.



    For other syscalls (listed in syscalls(2)), it is the same: use some program (or some builtin or some plugin) doing them. That program could be directly coded in assembler and would use SYSCALL or SYSENTER machine instruction to do the system call, or (and much more often) it would use your C standard library and use the function from libc doing that syscall. Executables don't need to be obtained from C source (for example, busybox is coded in assembler, the Scheme bones compiler don't use any libc). However, your libc is a cornerstone of your system.



    System calls changing some changeable and inheritable property of processes should be shell builtins (like cd for chdir(2), ulimit for setrlimit(2), etc...), because you might want to change the property in the shell process itself (and inherited by future command processes started by the shell). So if cd was a program it would only apply to the shell's child process running that program.



    BTW, system calls make only sense when done from some process. That process can either be the shell process or some child (or descendant) process started by the shell.



    Notice that Unix shells are ordinary programs. There are many of them (e.g. zsh, fish, scsh, es, etc ....)... It is an interesting exercise to code your own shell (and that can be done simply, see sash for an example; look also this for hints on globbing). Read something about Linux programming. If you are not happy with bash use another shell (perhaps changing your login shell using chsh(1)) or write your own one. Also, GNU bash is -like most other shells- free software. You can study its source code and improve it if you want to.






    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402834%2fexecute-system-calls-directly%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      9
      down vote













      User-space kernel-space communication via system calls is done in terms of memory locations and machine registers. That's way below the abstraction level of shells, which operate mainly with text strings.



      That said, in bash, you can use the https://github.com/taviso/ctypes.sh plugin to get through the text-string abstraction down to C-level granularity:



      $ . ctypes.sh
      $ dlcall -r long geteuid
      long:1001


      For this particular operation though, it would be much simpler, more idiomatic, and more efficient to simply use bash's magic $UID variable.



      $ echo "$EUID" #effectively a cached geteuid call
      1001





      share|improve this answer


























        up vote
        9
        down vote













        User-space kernel-space communication via system calls is done in terms of memory locations and machine registers. That's way below the abstraction level of shells, which operate mainly with text strings.



        That said, in bash, you can use the https://github.com/taviso/ctypes.sh plugin to get through the text-string abstraction down to C-level granularity:



        $ . ctypes.sh
        $ dlcall -r long geteuid
        long:1001


        For this particular operation though, it would be much simpler, more idiomatic, and more efficient to simply use bash's magic $UID variable.



        $ echo "$EUID" #effectively a cached geteuid call
        1001





        share|improve this answer
























          up vote
          9
          down vote










          up vote
          9
          down vote









          User-space kernel-space communication via system calls is done in terms of memory locations and machine registers. That's way below the abstraction level of shells, which operate mainly with text strings.



          That said, in bash, you can use the https://github.com/taviso/ctypes.sh plugin to get through the text-string abstraction down to C-level granularity:



          $ . ctypes.sh
          $ dlcall -r long geteuid
          long:1001


          For this particular operation though, it would be much simpler, more idiomatic, and more efficient to simply use bash's magic $UID variable.



          $ echo "$EUID" #effectively a cached geteuid call
          1001





          share|improve this answer














          User-space kernel-space communication via system calls is done in terms of memory locations and machine registers. That's way below the abstraction level of shells, which operate mainly with text strings.



          That said, in bash, you can use the https://github.com/taviso/ctypes.sh plugin to get through the text-string abstraction down to C-level granularity:



          $ . ctypes.sh
          $ dlcall -r long geteuid
          long:1001


          For this particular operation though, it would be much simpler, more idiomatic, and more efficient to simply use bash's magic $UID variable.



          $ echo "$EUID" #effectively a cached geteuid call
          1001






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 6 '17 at 16:07









          Stéphane Chazelas

          283k53521854




          283k53521854










          answered Nov 6 '17 at 15:09









          PSkocik

          17.1k24588




          17.1k24588






















              up vote
              5
              down vote













              To get the uid, write your own C program (or some shell plugin, if your shell accepts them; FYI zsh can have plugins, called modules.) or more simply run the id(1) command.



              For other syscalls (listed in syscalls(2)), it is the same: use some program (or some builtin or some plugin) doing them. That program could be directly coded in assembler and would use SYSCALL or SYSENTER machine instruction to do the system call, or (and much more often) it would use your C standard library and use the function from libc doing that syscall. Executables don't need to be obtained from C source (for example, busybox is coded in assembler, the Scheme bones compiler don't use any libc). However, your libc is a cornerstone of your system.



              System calls changing some changeable and inheritable property of processes should be shell builtins (like cd for chdir(2), ulimit for setrlimit(2), etc...), because you might want to change the property in the shell process itself (and inherited by future command processes started by the shell). So if cd was a program it would only apply to the shell's child process running that program.



              BTW, system calls make only sense when done from some process. That process can either be the shell process or some child (or descendant) process started by the shell.



              Notice that Unix shells are ordinary programs. There are many of them (e.g. zsh, fish, scsh, es, etc ....)... It is an interesting exercise to code your own shell (and that can be done simply, see sash for an example; look also this for hints on globbing). Read something about Linux programming. If you are not happy with bash use another shell (perhaps changing your login shell using chsh(1)) or write your own one. Also, GNU bash is -like most other shells- free software. You can study its source code and improve it if you want to.






              share|improve this answer


























                up vote
                5
                down vote













                To get the uid, write your own C program (or some shell plugin, if your shell accepts them; FYI zsh can have plugins, called modules.) or more simply run the id(1) command.



                For other syscalls (listed in syscalls(2)), it is the same: use some program (or some builtin or some plugin) doing them. That program could be directly coded in assembler and would use SYSCALL or SYSENTER machine instruction to do the system call, or (and much more often) it would use your C standard library and use the function from libc doing that syscall. Executables don't need to be obtained from C source (for example, busybox is coded in assembler, the Scheme bones compiler don't use any libc). However, your libc is a cornerstone of your system.



                System calls changing some changeable and inheritable property of processes should be shell builtins (like cd for chdir(2), ulimit for setrlimit(2), etc...), because you might want to change the property in the shell process itself (and inherited by future command processes started by the shell). So if cd was a program it would only apply to the shell's child process running that program.



                BTW, system calls make only sense when done from some process. That process can either be the shell process or some child (or descendant) process started by the shell.



                Notice that Unix shells are ordinary programs. There are many of them (e.g. zsh, fish, scsh, es, etc ....)... It is an interesting exercise to code your own shell (and that can be done simply, see sash for an example; look also this for hints on globbing). Read something about Linux programming. If you are not happy with bash use another shell (perhaps changing your login shell using chsh(1)) or write your own one. Also, GNU bash is -like most other shells- free software. You can study its source code and improve it if you want to.






                share|improve this answer
























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  To get the uid, write your own C program (or some shell plugin, if your shell accepts them; FYI zsh can have plugins, called modules.) or more simply run the id(1) command.



                  For other syscalls (listed in syscalls(2)), it is the same: use some program (or some builtin or some plugin) doing them. That program could be directly coded in assembler and would use SYSCALL or SYSENTER machine instruction to do the system call, or (and much more often) it would use your C standard library and use the function from libc doing that syscall. Executables don't need to be obtained from C source (for example, busybox is coded in assembler, the Scheme bones compiler don't use any libc). However, your libc is a cornerstone of your system.



                  System calls changing some changeable and inheritable property of processes should be shell builtins (like cd for chdir(2), ulimit for setrlimit(2), etc...), because you might want to change the property in the shell process itself (and inherited by future command processes started by the shell). So if cd was a program it would only apply to the shell's child process running that program.



                  BTW, system calls make only sense when done from some process. That process can either be the shell process or some child (or descendant) process started by the shell.



                  Notice that Unix shells are ordinary programs. There are many of them (e.g. zsh, fish, scsh, es, etc ....)... It is an interesting exercise to code your own shell (and that can be done simply, see sash for an example; look also this for hints on globbing). Read something about Linux programming. If you are not happy with bash use another shell (perhaps changing your login shell using chsh(1)) or write your own one. Also, GNU bash is -like most other shells- free software. You can study its source code and improve it if you want to.






                  share|improve this answer














                  To get the uid, write your own C program (or some shell plugin, if your shell accepts them; FYI zsh can have plugins, called modules.) or more simply run the id(1) command.



                  For other syscalls (listed in syscalls(2)), it is the same: use some program (or some builtin or some plugin) doing them. That program could be directly coded in assembler and would use SYSCALL or SYSENTER machine instruction to do the system call, or (and much more often) it would use your C standard library and use the function from libc doing that syscall. Executables don't need to be obtained from C source (for example, busybox is coded in assembler, the Scheme bones compiler don't use any libc). However, your libc is a cornerstone of your system.



                  System calls changing some changeable and inheritable property of processes should be shell builtins (like cd for chdir(2), ulimit for setrlimit(2), etc...), because you might want to change the property in the shell process itself (and inherited by future command processes started by the shell). So if cd was a program it would only apply to the shell's child process running that program.



                  BTW, system calls make only sense when done from some process. That process can either be the shell process or some child (or descendant) process started by the shell.



                  Notice that Unix shells are ordinary programs. There are many of them (e.g. zsh, fish, scsh, es, etc ....)... It is an interesting exercise to code your own shell (and that can be done simply, see sash for an example; look also this for hints on globbing). Read something about Linux programming. If you are not happy with bash use another shell (perhaps changing your login shell using chsh(1)) or write your own one. Also, GNU bash is -like most other shells- free software. You can study its source code and improve it if you want to.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 6 '17 at 16:45

























                  answered Nov 6 '17 at 14:33









                  Basile Starynkevitch

                  7,9081940




                  7,9081940



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402834%2fexecute-system-calls-directly%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?