1

I'm working on a project where I need to connect a very old serial terminal to a linux device (a Raspberry). The terminal uses 4800bps, 7 data bits and even parity. I have reconfigured the kernel command line to use these parameters and also the serial-getty service and I get a nice connection where I can login and use the system.

The problem comes whenever I use a command with sudo, all the data received by the terminal is garbage, it seems that the system is using different serial parameters when I use sudo, after the command ends the parameters seem to be restored again and continues working as expected...

How can I prevent these changes?

6
  • 1
    Does the root user have shell startup files that perform stty sane or other config commands? Commented May 26, 2024 at 2:31
  • @SottoVoce I have checked the profile and bashrc and there is no call to stty. I have added a stty at the top of /etc/profile and now at least I can have an interactive su terminal working but sudo still does the same. Also, I added some echo's to the profile and it seems that whatever does this is executed before the profile, and checking with strace bash I can't find any script executed prior to it... Any idea what could be executed before? Commented May 26, 2024 at 8:14
  • 1
    I suspect (but maybe I'm wrong) that use_pty may have something to do with the problem. See man 5 sudoers where it describes use_pty. Note regardless if it says the default is on or off (I believe the default changed from off to on around 1.9.14), your actual /etc/sudoers may specify Defaults use_pty xor Defaults !use_pty even out of the box. Commented May 26, 2024 at 8:22
  • @KamilMaciorowski BINGO! That was it! It was set as default in the sudoers file, commenting it made the thing work correctly ^_^ Please add your comment as response and I will accept it :) Thanks a lot. Commented May 26, 2024 at 8:33
  • 1
    (1) I like my answers to be educative, not just "do this voodoo". I cannot fully explain why exactly use_pty results in what you have observed, so I won't add an answer. I won't mind if you post an answer. There is no need to mention my input, keep the answer technical. (2) Read this: How does the use_pty sudoers option prevent a persistence attack?. Commented May 26, 2024 at 8:43

1 Answer 1

0

So, thanks to @KamilMaciorowski I got it working.

Long story short: the problem is caused by use_pty in the sudoers file, commenting the Default use_pty line that my sudoers file had made it to work correctly.

Long story: well, after reading the source code of os_io.c I checked were it did the piping of data between processes and how it changed when use_pty is enabled. In the case of use_pty being enabled it creates a pseudo-terminal and pipes the data through it. The thing is that when the slave pseudo-terminal is created it has a function called setup_terminal_slave and this function explicitly sets the data bits to 8:

___HIDDEN int setup_terminal_slave
   ___P((int slave_fd),
        (slave_fd)
int slave_fd;)
{
  struct termios tios;
 
  if (tcgetattr (slave_fd, &tios) >= 0)
    {
      tios.c_lflag &= ~(ECHO | ECHOCTL | ICANON | IEXTEN | ISIG);
      tios.c_iflag &= ~(BRKINT | INLCR | ICRNL | INPCK | ISTRIP | IXON | IXOFF);
      tios.c_cflag &= ~(CSIZE | PARENB | CLOCAL);
      tios.c_cflag |= (CS8 | HUPCL); // <---HERE, 8 data bits
#ifndef OCRNL
#define OCRNL 0
#endif
      tios.c_oflag &= ~(OPOST | ONLCR | OCRNL);
 
      if (tcsetattr (slave_fd, TCSANOW, &tios) >= 0)
        return 0;
    }
 
  return -1;
}

As my terminal uses 7 data bits and even parity the eight bit was being interpreted as the parity. breaking the text in the chars that should had a '1' as parity bit. And that exactly matches what I saw in the screen, not all the chars were broken, some were ok and others were just a blank square, so the chars that should had a '0' parity bit were shown ok and the ones that should had a '1' were shown as a "transmission error".

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.