2023年12月22日 星期五

Linux FunctionFS for USB

The general idea of FunctionFS

The idea is to delegate actual USB function implementation to userspace, using a filesystem interface (read()/write() etc). This way, USB out traffic (from host) is available at a file descriptor ready for reading, and USB in traffic (to host) is accepted at a file descriptor ready for writing.

...

More detailed idea of FunctionFS

Now that you know about endpoints, you can learn more about FunctionFS. From the point of view of the gadget it is yet another USB function available for composing a gadget from. But it is special, because each instance of FunctionFS provides an instance of a 'functionfs' filesystem to be mounted. If you mount it you will notice there is only one entry: ep0. Its name suggests it is associated with endpoint 0. Indeed it is. However, the good news is that you don't need to implement all the endpoint 0 handling, because most of that is already taken care of for you by the composite layer. You only need to handle control requests directed at your particular USB function. ep0 is also (ab)used to specify the endpoints and USB strings. FunctionFS is not considered ready for binding until endpoint descriptors and strings are written to ep0. The gadget as a whole is not ready for binding if any of its functions is not ready. After the descriptors and strings are written, FunctionFS creates appropriate number of ep<number> files for you to use to implement the desired data streams between the host and the device.

An example function can be found in kernel sources: tools/usb/ffs-test.c The file also contains an example of how to specify USB descriptors and strings to be written to ep0 to make FunctionFS ready. Actually I'm using its modified and simplified version, which passes to the host whatever the host writes to it. We will get to its code later in this post.

How FunctionFS works — The Linux Kernel documentation

From kernel point of view it is just a composite function with some unique behaviour. It may be added to an USB configuration only after the user space driver has registered by writing descriptors and strings (the user space program has to provide the same information that kernel level composite functions provide when they are added to the configuration).

This in particular means that the composite initialisation functions may not be in init section (ie. may not use the __init tag).

From user space point of view it is a file system which when mounted provides an "ep0" file. User space driver need to write descriptors and strings to that file. It does not need to worry about endpoints, interfaces or strings numbers but simply provide descriptors such as if the function was the only one (endpoints and strings numbers starting from one and interface numbers starting from zero). The FunctionFS changes them as needed also handling situation when numbers differ in different configurations.

When descriptors and strings are written "ep#" files appear (one for each declared endpoint) which handle communication on a single endpoint. Again, FunctionFS takes care of the real numbers and changing of the configuration (which means that "ep1" file may be really mapped to (say) endpoint 3 (and when configuration changes to (say) endpoint 2)). "ep0" is used for receiving events and handling setup requests.

When all files are closed the function disables itself.

What I also want to mention is that the FunctionFS is designed in such a way that it is possible to mount it several times so in the end a gadget could use several FunctionFS functions. The idea is that each FunctionFS instance is identified by the device name used when mounting.

One can imagine a gadget that has an Ethernet, MTP and HID interfaces where the last two are implemented via FunctionFS. On user space level it would look like this:

$ insmod g_ffs.ko idVendor=<ID> iSerialNumber=<string> functions=mtp,hid
$ mkdir /dev/ffs-mtp && mount -t functionfs mtp /dev/ffs-mtp
$ ( cd /dev/ffs-mtp && mtp-daemon ) &
$ mkdir /dev/ffs-hid && mount -t functionfs hid /dev/ffs-hid
$ ( cd /dev/ffs-hid && hid-daemon ) &

On kernel level the gadget checks ffs_data->dev_name to identify whether its FunctionFS is designed for MTP ("mtp") or HID ("hid").

If no "functions" module parameters is supplied, the driver accepts just one function with any name.

When "functions" module parameter is supplied, only functions with listed names are accepted. In particular, if the "functions" parameter's value is just a one-element list, then the behaviour is similar to when there is no "functions" at all; however, only a function with the specified name is accepted.

The gadget is registered only after all the declared function filesystems have been mounted and USB descriptors of all functions have been written to their ep0's.

Conversely, the gadget is unregistered after the first USB function closes its endpoints.


ADB daemon on top of FunctionFS


FunctionFS: initial implementation
This is the second version of a patch which demonstrates the possibility
of using adbd (Android Debug Bridge daemon) with a generic FunctionFS gadget
instead of a custom adb usb gadget in the Linux kernel. It contains changes
introduced after Benoit's review - thank you Benoit.

The patch adds a replacement usb access layer to adbd: it adds a new layer as
a copy of an existing usb_linux_client.c and introduces
FunctionFS support proper on top of the copied layer. The former usb access
method is still available, the method is chosen at compilation time by
setting USE_USB_FUNCFS variable to false in adb's Android.mk in the section
dedicated to adbd.

How to use on the target device:

$ insmod g_ffs.ko idVendor=<vendor ID> iSerialNumber=<some string>
$ mount -t functionfs adb /dev/usbgadget/adb -o uid=2000,gid=2000
$ ./adbd

The Linux kernel 3.2 release's drivers/usb/gadget/composite.c contains
module parameter definitions for idVendor and iSerialNumber and was
verified to work. "adb devices" issued on host will list <some string> as
the device name.

In case adbd does not switch its identity to uid=2000,gid=2000, respective
options are not required in the mount command above. It depends on adb
properties. If adbd is run standalone (no more Android stuff), then the
properties are set to their defaults which imply switching uid,gid to
2000,2000.

By default old behaviour is compiled in. This can be changed by setting
USE_USB_FUNCFS to true in Android.mk.

This patch requires a patch to bionic which adds <linux/usb_functionfs.h>
which is an exact copy of the relevant file in the linux kernel.

Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>

Change-Id: I4b42eb267ffa50fca7a5fba46f388a2f083e8b2d

2023年12月17日 星期日

Linux password

Source

* How to Setup Strong Password Policy on Ubuntu Linux
* DISABLE LINUX PAM MODULE FOR SPECIFIC USERS !


How to Setup Strong Password Policy on Ubuntu Linux

This brief tutorial shows students and new users how to set up a firm password policy on Ubuntu 18.04 | 16.04.

Out of the box, Ubuntu machines are not set up with an advanced password policy. Any password, including weak ones, can be used and never expire. This may not be secure in some environments.

To configure a more secure password policy and protect your users from hackers and intrusions, you need to take some steps, and this post shows you how to do that.

Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, and other devices.

When you’re ready to set up a firm password policy on Ubuntu, follow the steps below:

Ensure Passwords Expire

In most business environments, user passwords are configured to expire every 60 to 90 days. Desktop users are not configured like business environments so that a single password can be used forever.

If you want to configure Ubuntu to force users to change passwords regularly, you can run the commands below to open the login. defs file.

sudo nano /etc/login.defs

For example, if you want the account password to be changed every 60 days and several days before it changes again, edit the highlighted lines in the file.

You can also set the number of days a warning is given before a password expires.

# Password aging controls:
#
#       PASS_MAX_DAYS   Maximum number of days a password may be used.
#       PASS_MIN_DAYS   Minimum number of days allowed between password changes.
#       PASS_WARN_AGE   Number of days warning given before a password expires.
#
PASS_MAX_DAYS   60
PASS_MIN_DAYS   1
PASS_WARN_AGE   7
.

Save the file and exit.

Configure PAM Password Module

A PAM module called pam_pwquality can be included with Ubuntu to require strong passwords for system users. pam_pwquality performs several basic checks, just like the old pam_cracklib module, including not allowing passwords to include a username from the GECOS field, rejecting passwords with more than N number of characters, and many other password-related checks.

To install and use the pam_pwquality module, run the commands below:

sudo apt install libpam-pwquality cracklib-runtime

pam_pwquality main configuration file is at /etc/pam.d/common-password. Run the commands below to edit the file.

sudo nano /etc/pam.d/common-password

A reasonable password requirement will follow similar guidelines.

  • Allow N number of retries before returning error [retry=3]
  • Set a minimal password length [minlen=8]
  • Set N number of repeated characters [maxrepeat =3]
  • Password must have uppercase characters [ucredit = -1]
  • Password must have lowercase characters [dcredit=-1]
  • Reject password with account name found in GECOS [gecoscheck=1]

Edit the highlighted line and add some of the requirements above to enforce.

# here are the per-package modules (the "Primary" block)
password        requisite                       pam_pwquality.so retry=3 minlen=8 maxrepeat=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1 difok=3 gecoscheck=1 reject_username enforce_for_root

password        [success=1 default=ignore]      pam_unix.so obscure use_authtok try_first_pass sha512
# here's the fallback if no module succeeds
password        requisite                       pam_deny.so

Make the above changes that fit your environment. When done, reboot your machine, and the changes above should apply.

That’s it!

Conclusion:

This post showed you how to set up a firm password policy on Ubuntu 18.04 | 16.04. If you find errors in the above, please use the form below to report.


DISABLE LINUX PAM MODULE FOR SPECIFIC USERS !

For user case where you want to skip MultiFactorAuthentication / PAM module like google authentication validation code for specific users and keep it enabled for the rest, below solution can be used-
1) Create a user group on the Linux instance. We are going to disable MFA/PAM for users present in this new group-
sudo groupadd <groupname>
2) Create User or add existing user to newly created group-
sudo useradd <username>
sudo usermod -a -G <groupname> <username>

3) Edit /etc/pam.d/sshd file and add the below statement to skip PAM module for the newly created group-

auth [success=done default=ignore] pam_succeed_if.so user ingroup <groupname>

Optional-

If full access is required for this new group then add below line to visudo file-

%<groupname>ALL=(ALL)       NOPASSWD: ALL

When a user will be created and added to the new group, on first login MFA will be created but will not be enforced on further logins.

eMMC

 linux - Check health of eMMC - Super User

Linux pipe

Q: How to process input pipe in real time?
A: Linux pipe is buffered (4096? 65536? bytes)
     Solution#1:
Unix buffering delays output to stdout, ruins your day | TurnKey GNU/Linux (turnkeylinux.org)
fflush(stdout);
     Solution#2:

2023年12月13日 星期三

英文標點符號

英文標點符號大全 | Wall Street English


’Apostrophe撇號
() Bracket (英式) / Parentheses (美式)括號
 : Colon冒號
 , Comma逗號
 - Dash破折號
 … Ellipsis省略號
 ! Exclamation Mark (英式) / Point (美式)感嘆號
 . Full Stop (英式) / Period (美式)句號
 《 》 Guillemets書名號
 – Hyphen連字號
?  Question Mark問號
 "" Quotation Mark引號
;  Semicolon分號
/ Slash斜線


[英文] 標點符號翻譯 Punctuation Marks @ 搗蛋鬼 扭扭 :: 痞客邦 :: (pixnet.net)

.

點

dot

 

-

破折號/中線

dash

。

句號

period

/

斜線

virgule ; slash

,

逗號

comma

\

反斜線

backslash

:

冒號

colon

~

波浪號

tilde (or swung dash)

;

分號

semicolon

...

省略號

ellipsis

!

驚嘆號

exclamation

( )

圓括號

parentheses

@

小老鼠

at

'  '

單引號

single quotation marks

?

問號

question mark

“ ”

雙引號

double quotation marks

'

所有格號

apostrophe

《》

法文引號或書名號

French quotes

*

星號 /米字 

asterisk (or star)

[ ]

方括號

(square) brackets

§

分節號

section ; division

{ }

大括號

braces

→

箭號

arrow

‖

雙線號

parallel

2023年12月10日 星期日

Linux coredump

int main(void)
{
    char *ptr = NULL;
    *ptr = 'A'; // This will cause a segmentation fault 
    return 0;
}

root@A9x28:/# ulimit -a | grep core
-c: core file size (blocks)        unlimited

root@A9x28:/# sysctl -a
fs.suid_dumpable = 2
kernel.core_pattern = /data/core

root@A9x28:/# cat /proc/sys/kernel/core_pattern
/data/core

root@A9x28:/# ./create_coredump
Segmentation fault (core dumped)

2023年11月16日 星期四

RFX (RFQ RFP RFI)

RFX

因應客戶開發時期的不同階段及與供應商的不同合作關係(ODM/OEM/EMS), 客戶會對於他所認為夠格(Qualified)的供應商發出RFP/RFI/RFQ等不同產品建議或報價邀請, 對於我所知道的科技相關產品行業而言, 其間的差別及因應事項分享如下:

[RFQ]=Request for Quotation(報價邀請書), 此為最常見的邀請報價形式.通常客戶在此RFQ文件中已經詳細定義了求產品規格, 品質要求,預計應用,驗證方式,出貨包裝,交貨數量及出貨日期等等,然後發出該RFQ給若干供應商. 而身為供應商的我方, 應該依照客戶需求的各要項依序列示我方產品的符合程度, 或提出更 佳建議方案, 並提出最具競爭力的價格方案. 通常回覆時除了報價單外, 另外會有一方完整的我方準備的回覆文件(ppt file)說明我方準備的產品計劃及對於RFQ各項要件的說明.

[RFP/RFI] RFP=Request for Proposal; RFI= Request for Information; 實務上, 對於我所操作及回覆客戶的實際經驗, RFP/RFI兩者之間可以視為同一種客戶邀請....