Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: execve() gives ": unknown program '''" if you run ./a.out once you compile with clang/g++ #20242

Closed
SwuduSusuwu opened this issue May 17, 2024 · 13 comments
Labels
invalid not-bug Issue is not a bug or has been resolved

Comments

@SwuduSusuwu
Copy link

Problem description

~/ $ cat << EOF > test.cxx
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *const* _args) {
  char *args[2];
  printf("1");
  args[0] = "/bin/ls";
  args[1] = "-lh";
  execve(args[0], args, NULL);
  args[1] = "";
  execve(args[0], args, NULL);
  execve(args[0], NULL, NULL);
  args[0] = "/bin/echo";
  args[1] = "u";
  execve(args[0], args, NULL);
  printf("2");
return 0;
}
EOF
~/ $ g++ test.cxx && ./a.out
: unknown program '''
Try ' --help' for more information
~/ $ cat << EOF > test.cxx
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *const* _args) {
  char *args[2];
  printf("1");
  args[0] = "/bin/ls";
  args[1] = "-lh";
  execve(args[0], args, NULL);
  args[1] = "";
  execve(args[0], args, NULL);
  args[0] = "/bin/echo";
  args[1] = "u";
  execve(args[0], args, NULL);
  printf("2");
return 0;
}
EOF
~/ $ g++ test.cxx && ./a.out
12
~/ $

What steps will reproduce the bug?

Install latest Termux from Google Store, install latest clang from official repos on Termux

What is the expected behavior?

Should output directory contents as list

System information

Android 14
Termux 1.42.1
@SwuduSusuwu SwuduSusuwu added bug report Something is not working properly untriaged labels May 17, 2024
@SwuduSusuwu
Copy link
Author

Have removed lots to reduce this.
Original sources: https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/VirusAnalysis.cxx

@twaik
Copy link
Member

twaik commented May 17, 2024

Playstore build is deprecated and should not be used. Use F-droid or github release.

@SwuduSusuwu
Copy link
Author

Am not allowed to use this phone to install from third-party sources.
But am curious, which of the new versions fixes this?

@Grimler91
Copy link
Member

Seems pretty obvious that error comes from:

execve(args[0], NULL, NULL);

What do you expect execve to do with NULL as argv[]?

@TomJo2000 TomJo2000 added invalid not-bug Issue is not a bug or has been resolved and removed bug report Something is not working properly untriaged labels May 17, 2024
@TomJo2000 TomJo2000 closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
@SwuduSusuwu
Copy link
Author

Seems pretty obvious that error comes from:

execve(args[0], NULL, NULL);

What do you expect execve to do with NULL as argv[]?

Used all possible variations, all failed.
From searches for examples of how to use exec* functions, commands such as /bin/ls without args should run for us.
Which variation (with the third party sources) runs for you?
If the third-party versions of Termux can do this,
could argue to allow third-party sources.

@TomJo2000
Copy link
Member

TomJo2000 commented May 17, 2024

This isn't a "third party" issue.
(Additionally I'd like to note that the F-Droid and GitHub APK releases are 1st party distribution methods of Termux.)

What you seem to be dealing with is a fundamental lack of experience with the C programming language.
Unrelated to the version or distribution channel of Termux you are making use of.

@SwuduSusuwu
Copy link
Author

SwuduSusuwu commented May 17, 2024

This isn't a "third party" issue. (Additionally I'd like to note that the F-Droid and GitHub APK releases are 1st party distribution methods of Termux.)

What you seem to be dealing with is a fundamental lack of experience with the C programming language. Unrelated to the version or distribution channel of Termux you are making use of.

The top result on Google (for how to use C/C++ to execute Bash): https://stackoverflow.com/a/30149941/24473928

int main() {
        char *args[2];
        args[0] = "/bin/ls";
        args[1] = "-lh";
        execve(args[0], args, NULL);
}

Passes args to /bin/ls,
but does not output lists (as #20242 shows,) so used variations (such as without args,) which also do not output. Even /bin/echo does not output.
Are you saying that a new version of Termux fixes this, and, if so, which version of Termux fixes this?
Or are you saying that all the results for how to do this were from idiots and that none of them should function on any implementation of Bash/Ash?

Lots of businesses do not allow you to use devices with apps that do not from Google app stores: https://natlawreview.com/article/mitigating-rising-risk-corporate-use-third-party-apps
but if new versions of Termux improve functions for businesses,
could convince bosses to allow this.

@agnostic-apollo
Copy link
Member

agnostic-apollo commented May 17, 2024

What are you even on about, read the comment by grimler above.

You are passing NULL as argv in execve(args[0], NULL, NULL);, when infact you should be passing the path to the executable you need to execute as the first element of the argv array (you name it as args). That's why you are getting the unknown program error or would possibly get a segmentation fault.

Your second issue is that your args array is of size 2, when infact it needs to be size 3 and its final element needs to be NULL so that it's null terminated, so that the kernel knows when to stop reading arguments in the array, it has no way of knowing the initial size you created the array, so it doesn't know how many args there are. You need to declare char *args[3]; and set final element as args[2] = NULL; if passing only 1 arg to the executable. If you are passing additional args, adjust array size and final index accordingly. Fix that and ls and echo will work.

Read the manpage for execve()

The char *const argv[] argument is an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by a null pointer.

https://man7.org/linux/man-pages/man3/exec.3.html

Basically, these are not issues with termux (playstore or fdroid/github), but your own code. The only issue related to exec() you may get is shebangs or exectuable path correction not working on Android 14 from /usr/bin/* to $TERMUX__PREFIX/bin/* paths. For that, install the termux-exec package in the zip attached in the comment at termux/termux-exec#24 (comment)

but if new versions of Termux improve functions for businesses,
could convince bosses to allow this.

There have been 10 app version releases on fdroid/github since last playstore release, with massive addition in changes and bug and security/vulnerability fixes. We in no way recommend installing termux from playstore or support it.

https://github.com/termux/termux-app#google-play-store-deprecated

@agnostic-apollo
Copy link
Member

agnostic-apollo commented May 18, 2024

https://stackoverflow.com/a/30149941/24473928

I just checked and there is already a comment under it for "You need NULL in the last element of args", same as I said. Answers at stackoverflow, etc should not be used without understanding what they are doing or trusting them blindly, there are tonne of wrong and dangerous answers there. Always read official docs and man pages when running things.

@SwuduSusuwu
Copy link
Author

SwuduSusuwu commented May 18, 2024

Cool, bless you
char *args = {"/bin/ls", "-lh", NULL}; no output (do not want "-lh", but example has this, and curious what the problem is)
char *args = {"/bin/ls", "", NULL}; success
char *args = {"/bin/ls", NULL, NULL}; success
char *args = {"/bin/echo", "test", NULL}; success
(for execve(args[0], args, NULL)

To install fdroid.apk (or termux.apk) gives this error (and most businesses do not allow you to use your own device if you change this):
Screenshot_20240518-071419
Hope termux/termux-app#2155 resolves this

@twaik
Copy link
Member

twaik commented May 18, 2024

and most businesses do not allow you to use your own device if you change this

What exactly do you mean?

@agnostic-apollo
Copy link
Member

@agnostic-apollo
Copy link
Member

char *args = {"/bin/ls", "-lh", NULL}; no output (do not want "-lh", but example has this, and curious what the problem is)

Use char *args[] = {"/bin/ls", "-lh", NULL};, not char *args =....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid not-bug Issue is not a bug or has been resolved
Projects
None yet
Development

No branches or pull requests

5 participants