Protecting the term from being abused by others using TIOCSLCKTRMIOS

Ray Strode halfline at gmail.com
Thu Mar 12 08:16:00 PDT 2009


Hi,

On Wed, Mar 11, 2009 at 7:20 AM, Charlie Brej <plymouth at brej.org> wrote:
> I tried to locate which program was flipping ECHO etc during the boot but
> eventually gave up. Trying to flock the tty didn't help either. Looks like
> TIOCSLCKTRMIOS is the most sensible. Attached is a patch, which does this.
So one thing I'm worried about is how other apps behave if the tty is locked.

Do they error out if they can't make the changes they want? Is boot up smooth?

I also wonder how this works on serial consoles.

> @@ -417,12 +418,19 @@ static bool
>  ply_window_set_unbuffered_input (ply_window_t *window)
>  {
>    struct termios term_attributes;
> +  struct termios term_attribute_locks;
>
> -  tcgetattr (window->tty_fd, &term_attributes);
> +  if (tcgetattr (window->tty_fd, &term_attributes) < 0 )
> +    return false;
> +  if (ioctl (window->tty_fd, TIOCGLCKTRMIOS, &term_attribute_locks) < 0 )
> +    return false;
>
>    if (!window->original_term_attributes_saved)
>      {
> +      if (term_attributes.c_lflag & ECHO)
> +        ply_trace ("echo flag has been altered by another program");
Left over debug spew?  Can't happen with this patch right?

>        window->original_term_attributes = term_attributes;
> +      window->original_term_attribute_locks = term_attribute_locks;
>        window->original_term_attributes_saved = true;
>      }
>
> @@ -431,8 +439,14 @@ ply_window_set_unbuffered_input (ply_window_t *window)
>    /* Make \n return go to the beginning of the next line */
>    term_attributes.c_oflag |= ONLCR;
>
> +  term_attribute_locks.c_iflag |= IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON;
> +  term_attribute_locks.c_oflag |= OPOST;
> +  term_attribute_locks.c_lflag |= ECHO | ECHONL | ICANON | ISIG | IEXTEN;
This is a bit ugly.  I guess at a minimum we should hide this behind
three preprocessor defines... e.g.

#define PLY_WINDOW_TERMINAL_INPUT_ATTRIBUTE_MASK
#define PLY_WINDOW_TERMINAL_OUTPUT_ATTRIBUTE_MASK
#define PLY_WINDOW_TERMINAL_LOCAL_ATTRIBUTE_MASK

and have a comment that says "These are documented in the cfmakeraw man page"

Another idea would be to try to infer the mask based on what bits
cfmakeraw changes versus what the bits were originally.

Something like:

/* We want all the bits that cfmakeraw sets, set in our mask */
input_mask = term_attributes.c_iflag;

/* We also want all the bits that cfmakeraw clears, set in our mask */
input_mask |= (original_term_attributes.c_iflag ^ term_attributes.c_iflag);

Not sure if that logic is right.  Or maybe we should just lock everything?

--Ray


More information about the plymouth mailing list