Enabling Password-Protected Login

Enabling Password-Protected Login

In the default embedded project available with the Emcraft uClinux BSP the serial console does not require a password. This application note explains how to add login and password protection to the Linux serial console.

The busybox tool has the passwd, getty and login applets. login is already enabled in the default configuration; passwd and getty are not. On the host, run make bmenuconfig in the project directory to enable CONFIG_PASSWD and CONFIG_GETTY, then edit etc/inittab in the project directory, replacing the line that respawns the shell directly (::respawn:-/bin/hush) with a getty:

::sysinit:/etc/rc ::respawn:/sbin/getty -L 0 ttyLP0

ttyLP0 is the LPUART1 serial console on the i.MX RT. The -L option makes getty ignore the Carrier Detect state, and a baud rate of 0 leaves the line speed unchanged. getty prompts for the login name and then invokes /bin/login, which authenticates the user against /etc/shadow.

Rebuild the embedded project and boot it to the target. The updated project will support the following credentials:

login: root
password: 123

Where the Credentials Come From

The /etc/passwd, /etc/group and /etc/shadow files are installed into the image by the project's .initramfs file. By default they are taken from the project directory:

file /etc/passwd ${INSTALL_ROOT}/projects/${SAMPLE}/etc/passwd 644 0 0 file /etc/group ${INSTALL_ROOT}/projects/${SAMPLE}/etc/group 644 0 0 file /etc/shadow ${INSTALL_ROOT}/projects/${SAMPLE}/etc/shadow 644 0 0

Buildroot builds its own /etc/passwd, /etc/group and /etc/shadow under ${BUILDROOT_TARGET}, but with the lines above in place those files are never installed. Setting a root password in the buildroot configuration alone therefore has no effect on the image.

The two sections below describe the two ways of managing the credentials. The first one is the default.

1. Credentials in the Project Directory

This is the default arrangement and requires no change to the project.

To change the password, log in to the board, run passwd root, then copy-paste the content of /etc/shadow from the target to the etc/shadow file in your project directory on the development host. The next build of the embedded project on the development host will pick up the new password into the bootable Linux image.

The credentials are part of the project, so different projects can carry different passwords, and changing a password rebuilds nothing but the image itself.

2. Credentials Managed by Buildroot

This requires the BSP to be activated with a buildroot defconfig, that is, with a buildroot defconfig name passed as the second argument to ACTIVATE.sh. Refer to the Using Buildroot to Customize the Toolchain and Applications document supplied with the BSP distribution for details.

Configure the credentials in buildroot. In the project directory run:

$ make buildroot-menuconfig

Under System configuration, set the following:

  • Passwords encoding - the algorithm used to encode clear-text passwords. The default is sha-256; md5 and sha-512 are also available. All three are supported by the uClibc library on the target.

  • Root password - the password for root. A value beginning with $1$, $5$ or $6$ is taken to be an already encoded md5, sha-256 or sha-512 hash; any other value is treated as clear text and is encoded at build time. In an already encoded value, every $ sign must be doubled, so that make does not interpret it as a variable reference.

  • Path to the users tables - optional; a file describing additional users, one user per line, as a space-separated list of fields:

username uid group gid password home shell groups comment

The password field is a crypt(3) hash, or a clear-text value prefixed with =, or - for no password at all. Refer to buildroot/docs/manual/makeusers-syntax.txt for the full description of the fields.

The session saves the result back to the buildroot defconfig when you exit.

Buildroot builds its own /etc/passwd, /etc/group and /etc/shadow under ${BUILDROOT_TARGET}, but the project installs the files from its own directory, so configuring a password in buildroot alone has no effect on the image. Point the project at the buildroot files instead: in <project>.initramfs, replace the three lines quoted above with:

file /etc/passwd ${BUILDROOT_TARGET}/etc/passwd 644 0 0 file /etc/group ${BUILDROOT_TARGET}/etc/group 644 0 0 file /etc/shadow ${BUILDROOT_TARGET}/etc/shadow 644 0 0

Finally rebuild the project:

$ make

The modified buildroot defconfig triggers a rebuild of the buildroot tree, after which the new credentials are installed into the image.