termux-packages icon indicating copy to clipboard operation
termux-packages copied to clipboard

in an unknown edge case, `bash` may become corrupted by each login unless forced read-only

Open HackManager opened this issue 3 months ago • 21 comments

Today I noticed that for some reason Termux started using "sh", but I always use "bash". I did this:

  • used "chsh -s zsh" to change terminal shell into "zsh", successfully
  • found out, that /bin/bash doesn't have rights to execute
  • want to return "bash"
  • making "chmod +x bash" in /bin/ directory
  • than "chsh -s bash"
  • reloading Termux Then I have:
/data/data/com.termux/files/usr/bin/bash: 1: Syntax error: ")" unexpected

[Process completed (code 2) - press Enter]

Can I fix it without rooting my device? Have some data inside Termux need to save.

Termux 0.118.3 Android 11 Realme C 21Y

HackManager avatar Sep 05 '25 10:09 HackManager

You can open a fallback session by long pressing on the Termux app icon. This should allow you to chsh the shell of your regular session back to a working shell.

Just to confirm you changed your login shell to /bin/bash and it broke?

TomJo2000 avatar Sep 05 '25 10:09 TomJo2000

Just to confirm you changed your login shell to /bin/bash and it broke?

It's weird, but yes.

I tried in failsafe mode return to "zsh", but:

:/data/data/com.termux/files/home $ chsh -s zsh
sh: chsh: inaccessible or not found
127|:/data/data/com.termux/files/home $

HackManager avatar Sep 05 '25 11:09 HackManager

@HackManager please try this command in Termux Failsafe Shell:

rm /data/data/com.termux/files/home/.termux/shell

that should reset any accidental misconfiguration of chsh to default.

robertkirkman avatar Sep 05 '25 11:09 robertkirkman

@HackManager If the problem is that your /data/data/com.termux/files/usr/bin/bash is not working, so that does not help, please try this in Termux Failsafe Shell to return to zsh:

ln -sf /data/data/com.termux/files/usr/bin/zsh /data/data/com.termux/files/home/.termux/shell

then, after that, please try to reinstall bash using:

pkg upgrade
pkg reinstall bash

robertkirkman avatar Sep 05 '25 11:09 robertkirkman

@HackManager please try this command in Termux Failsafe Shell:

rm /data/data/com.termux/files/home/.termux/shell

that should reset any accidental misconfiguration of chsh to default.

Thank you! I have returned in zsh

But something wrong with pkg:

zsh: exec format error: pkg

HackManager avatar Sep 05 '25 11:09 HackManager

I reinstalled bash with apt successfully. But it doesn't see pkg:

% apt reinstall pkg
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package pkg

HackManager avatar Sep 05 '25 11:09 HackManager

I reinstalled bash with apt successfully. But it doesn't see pkg:

% apt reinstall pkg
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package pkg

These commands should help your problem:

apt update
apt upgrade
apt reinstall bash termux-tools

the reason why apt reinstall pkg does not work is because "pkg" is not the name for pkg command in apt install. It is "termux-tools" when specified in apt commands, not "pkg".

robertkirkman avatar Sep 05 '25 11:09 robertkirkman

Thank you all very much! For help and including for explaining an issue. I really need to always understand what's happening in system. I liked the move with ln. Did I understand right that it is the manual way to do the chsh work?

PS sorry, lost sight of pkg is part of termux-tools

HackManager avatar Sep 05 '25 12:09 HackManager

Did I understand right that it is the manual way to do the chsh work?

Yes! that is basically how chsh works but written manually, but normally chsh has a lot of checks in it to make sure that you don't accidentally set the shell to an invalid state.

However, in this case, something seems to have happened that corrupted your /data/data/com.termux/files/usr/bin/bash file, which prevented you from using any of the regular commands, and you needed to use the manual ln command in the failsafe shell, and then apt reinstall bash in the zsh shell, in order to fix it.

robertkirkman avatar Sep 05 '25 15:09 robertkirkman

Thanks, I understand!

However, in this case, something seems to have happened that corrupted your bash file

And yes, you right. Because bash stops to work after Termux reboot, or using any shell script, or starting utility, mc for example.

It seems to me something changes the execution rights for bash. And it's weird because I did not install any packages, didn't make any changes in config files recently, and use official repos only.

Have to remain on zsh until I understand what's happening.

HackManager avatar Sep 05 '25 17:09 HackManager

And yes, you right. Because bash stops to work after Termux reboot, or using any shell script, or starting utility, mc for example.

It seems to me something changes the execution rights for bash. And it's weird because I did not install any packages, didn't make any changes in config files recently, and use official repos only.

Have to remain on zsh until I understand what's happening.

I have experienced that issue previously under certain conditions in 2024,

and I do know of one potential workaround, even though my workaround is not very good!

My only currently known successful workaround to this problem (requires the device to be rooted)

apt reinstall bash
su
chattr +i /data/data/com.termux/files/usr/bin/bash
exit

Unfortunately, I don't yet understand what truly causes it, but this is very exciting because you are the first other person I have ever seen experience this problem!

This means that if I or others try investigating further, we might be able to figure out the root cause and make a permanent and proper solution using our findings.

Since I was unable to figure out what causes the problem, I stored a custom build of Termux in September 2024 that has preserved a snapshot of this issue, and is affected by the issue consistently every single time it is opened, making this issue technically consistently reproducible in at least a form that exactly matches your description

https://github.com/owokitty/termux-app-retired64/releases/tag/retired64-test-2

this means that whatever causes this issue, can almost certainly be found somewhere both inside your Termux and also inside my Retired64 Termux v2.

[!CAUTION] Retired64 Termux v2 is absolutely huge and is about 22 GB fully installed. It is also affected by security problems because it uses old packages that cannot be updated in-place. Therefore, I recommend that it should not be installed by anyone especially not on devices that are connected to the internet, and instead it is safer for anyone helping with this who wants to check Retired64 Termux v2, to investigate the problem by extracting its bootstrap without installing or running it, and searching through its files for possible root causes to this problem.

robertkirkman avatar Sep 05 '25 23:09 robertkirkman

In my original Retired64 Termux v2 release description, I said:

report bugs here, not at termux.dev

however, we can see that unfortunately, you encountered an extremely similar problem somehow even though you are using a com.termux F-Droid termux, not a custom Termux. That is why it turns out that since the issue must be somehow reproducible in official Termux after all, that means that both official Termux and my Retired64 Termux v2 are affected.

robertkirkman avatar Sep 05 '25 23:09 robertkirkman

Today I noticed that for some reason Termux started using "sh", but I always use "bash".

Everything, even down to what you originally saw happening, is, as far as I can remember, perfectly matched and reproducible as the default behavior of Retired64 Termux v2.

robertkirkman avatar Sep 05 '25 23:09 robertkirkman

One idea of a safe way that Retired64 Termux v2 might be helpful for attempting to reproduce this issue in official Termux:

I stored a mostly-complete list of almost every single package that was built into Retired64 Termux v2 here:

https://github.com/owokitty/termux-app-retired64/blob/retired64v2/app/src/main/assets/bootstrap-aarch64.txt

One hypothesis is, maybe the issue's root cause can be found somewhere inside a package, or combination of packages, that is not frequently installed.

If that hypothesis is true, then maybe an Android device or Android virtual device with a lot of space available can be used to install that entire package list using pkg install in official Termux, then restarted to check whether simply installing a lot of packages is a possible way to reproduce the problem or not.

robertkirkman avatar Sep 05 '25 23:09 robertkirkman

I would like to help. What kind of data is needed from my side?

HackManager avatar Sep 08 '25 13:09 HackManager

do you have a device that has at least 30-40 gb of free space you can use and that you can uninstall termux and clean install it into?

i want to check to see if installing all of those packages from the list I last sent into an official com.termux Termux is a way to reproduce the problem,

but it will require a lot of space and a lot of time to download and extract all 20 GB of packages, so i need to set up a device specifically for this test and wait for it to run.

robertkirkman avatar Sep 08 '25 13:09 robertkirkman

At this moment I have only one main mobile and don't have any other devices. But I can upload recent apt history, names of installed for now pkges, damaged and original bash pkges for analysis. If I will have some android VM in near future, I'll install your version.

HackManager avatar Sep 08 '25 14:09 HackManager

could you upload a complete log of this command on your affected device to a pastebin? It might help.

apt list --installed

robertkirkman avatar Sep 08 '25 14:09 robertkirkman

I have already done. You can see everything in my "trmxBadBash" repo.

HackManager avatar Sep 08 '25 15:09 HackManager

I tested clean installing Termux and then installing all of the packages from your list, and unfortunately, I wasn't able to reproduce the problem by doing that.

This means that most likely, the cause of the problem is not directly related to the official default clean installation state of any specific package, but is something more obscure and difficult to reproduce that cannot be reproduced by simply installing a lot of packages in the normal way.

Retired64 Termux remains the only currently-known environment where this bug is cleanly reproducible out of the box, so it is definitely something that can eventually be found by closely inspecting every line of code and every file inside Retired64 Termux until the cause is found, eventually I will reinstall Retired64 Termux on a device and use it to try to find the cause.

robertkirkman avatar Sep 25 '25 21:09 robertkirkman

I see. Maybe it's worth starting with the difference in damaged and original files?

HackManager avatar Sep 27 '25 10:09 HackManager