Version 10 (2.6.24-rc1) Smack: Simplified Mandatory Access Control Kernel
Subsystems (4)
Casey Schaufler
maintainerPATCH 1/1 Version 10 (2.6.24-rc1) Smack: Simplified Mandatory Access Control Kernel
From: Casey Schaufler <[email protected]>
Smack is the Simplified Mandatory Access Control Kernel.
Smack implements mandatory access control (MAC) using labels attached to tasks and data containers, including files, SVIPC, and other tasks. Smack is a kernel based scheme that requires an absolute minimum of application support and a very small amount of configuration data.
Smack uses extended attributes and
provides a set of general mount options, borrowing technics used elsewhere. Smack uses netlabel for CIPSO labeling. Smack provides a pseudo-filesystem smackfs that is used for manipulation of system Smack attributes.
The patch, patches for ls and sshd, a README, a startup script, and x86 binaries for ls and sshd are also available on
This version has been tested with 2.6.24-rc1. Development has been done using Fedora Core 7 in a virtual machine environment and on an old Sony laptop. There are pending changes required to the netlabel api patch for 2.6.24-rc1-git11 (perhaps earlier).
Smack provides mandatory access controls based on the label attached to a task and the label attached to the object it is attempting to access. Smack labels are deliberately short (1-23 characters) text strings. Single character labels using special characters are reserved for system use. The only operation applied to Smack labels is equality comparison. No wildcards or expressions, regular or otherwise, are used. Smack labels are composed of printable characters and may not include "/".
A file always gets the Smack label of the task that created it.
Smack defines and uses these labels:
"*" - pronounced "star"
"_" - pronounced "floor"
"^" - pronounced "hat"
"?" - pronounced "huh"The access rules enforced by Smack are, in order:
1. Any access requested by a task labeled "*" is denied.
2. A read or execute access requested by a task labeled "^" is permitted.
3. A read or execute access requested on an object labeled "_" is permitted.
4. Any access requested on an object labeled "*" is permitted.
5. Any access requested by a task on an object with the same label is permitted.
6. Any access requested that is explicitly defined in the loaded rule set is permitted.
7. Any other access is denied.
Rules may be explicitly defined by writing subject,object,access triples to /smack/load.
Smack rule sets can be easily defined that describe Bell&LaPadula sensitivity, Biba integrity, and a variety of interesting configurations. Smack rule sets can be modified on the fly to accomodate changes in the operating environment or even the time of day.
Some practical use cases:
Hierarchical levels. The less common of the two usual uses for MLS systems is to define hierarchical levels, often unclassified, confidential, secret, and so on. To set up smack to support this, these rules could be defined:
C Unclass rx S C rx S Unclass rx TS S rx TS C rx TS Unclass rx
A TS process can read S, C, and Unclass data, but cannot write it. An S process can read C and Unclass. Note that specifying that TS can read S and S can read C does not imply TS can read C, it has to be explicitly stated.
Non-hierarchical categories. This is the more common of the usual uses for an MLS system. Since the default rule is that a subject cannot access an object with a different label no access rules are required to implement compartmentalization.
A case that the Bell & LaPadula policy does not allow is demonstrated with this Smack access rule:
A case that Bell&LaPadula does not allow that Smack does:
ESPN ABC r
ABC ESPN rOn my portable video device I have two applications, one that shows ABC programming and the other ESPN programming. ESPN wants to show me sport stories that show up as news, and ABC will only provide minimal information about a sports story if ESPN is covering it. Each side can look at the other's info, neither can change the other. Neither can see what FOX is up to, which is just as well all things considered.
Another case that I especially like:
SatData Guard w
Guard Publish w
A program running with the Guard label opens a UDP socket and accepts messages sent by a program running with a SatData label. The Guard program inspects the message to ensure it is wholesome and if it is sends it to a program running with the Publish label. This program then puts the information passed in an appropriate place. Note that the Guard program cannot write to a Publish file system object because file system semanitic require read as well as write.
The four cases (categories, levels, mutual read, guardbox) here are all quite real, and problems I've been asked to solve over the years. The first two are easy to do with traditonal MLS systems while the last two you can't without invoking privilege, at least for a while.
---
This patch depends on the netlabel interface patch by Paul Moore <[email protected]> in support of a more comfortable interface to initialize the CIPSO code from within the kernel. This patch was titled "[NetLabel] Introduce a new kernel configuration API for NetLabel".
This update includes:
- Ahmed Darwish's fixes for smackfs read operations. Really has them this time.
- Cleaned out a bunch of unnecessary NULL checking
- Fixed some hooks that would not account for important special cases.
- Deleted file system hooks that were making nothing but unnecessary access checks.
- Removed the SMACK64PACKET attribute from sockets and all the code associated with maintaining it.
- Provided the hooks to support SCM_SECURITY and SO_PEERSEC.
- Pulled the bulky comment about what hooks are not used.
Still to come:
- Final cleanup of smack_load_write and smack_cipso_write.
- Another pass through the hooks to ensure the behavior is really correct given the lessons learned to date.
- Investigation into an NFS oops on an older version.
- Filesystem magic number migration
Special Thanks:
- Al Viro, for all the help with locking and seq_file.
- Stephen Smalley, for his insightful comments on the hooks.
- Ahmed Darwish, for code I didn't have to write
- Everyone who's mentioned unexpected "behaviors".
Thank you.
Documentation/Smack.txt | 478 ++++++ security/Kconfig | 1 security/Makefile | 2 security/smack/Kconfig | 10 security/smack/Makefile | 7 security/smack/smack.h | 222 ++ security/smack/smack_access.c | 353 ++++ security/smack/smack_lsm.c | 2529 ++++++++++++++++++++++++++++++++ security/smack/smackfs.c | 932 +++++++++++ 9 files changed, 4534 insertions(+)
Documentation/Smack.txt +478 -0
@@ -0,0 +1,478 @@ + + + "Good for you, you've decided to clean the elevator!" + - The Elevator, from Dark Star + +Smack is the the Simplified Mandatory Access Control Kernel. +Smack is a kernel based implementation of mandatory access +control that includes simplicity in its primary design goals. + +Smack is not the only Mandatory Access Control scheme +available for Linux. Those new to Mandatory Access Control +are encouraged to compare Smack with the other mechanisms +available to determine which is best suited to the problem +at hand. + +Smack consists of three major components: + - The kernel + - A start-up script and a few modified applications + - Configuration data + +The kernel component of Smack is implemented as a Linux +Security Modules (LSM) module. It requires netlabel and +works best with file systems that support extended attributes, +although xattr support is not strictly required. +It is safe to run a Smack kernel under a "vanilla" distribution. +Smack kernels use the CIPSO IP option. Some network +configurations are intolerant of IP options and can impede +access to systems that use them as Smack does. + +The startup script etc-init.d-smack should be installed +in /etc/init.d/smack and should be invoked early in the +start-up process. On Fedora rc5.d/S02smack is recommended. +This script ensures that certain devices have the correct +Smack attributes and loads the Smack configuration if +any is defined. + +A version of "ls" that provides a "-M" option to display +Smack labels on long listing is available. + +A hacked version of sshd that allows network logins by users +with specific Smack labels is available. This version does +not work for scp. You must set the /etc/ssh/sshd_config +line: + UsePrivilegeSeparation no + +The format of /etc/smack/usr is: + + username smack + +In keeping with the intent of Smack, configuration data is +minimal and not strictly required. The most important +configuration step is mounting the smackfs pseudo filesystem. + +Add this line to /etc/fstab: + + smackfs /smack smackfs smackfsdef=* 0 0 + +and create the /smack directory for mounting. + +Smack uses extended attributes (xattrs) to store file labels. +The command to set a Smack label on a file is: + + # attr -S -s SMACK64 -V "value" path + +NOTE: Smack labels are limited to 23 characters. The attr command + does not enforce this restriction and can be used to set + invalid Smack labels on files. + +If you don't do anything special all users will get the floor ("_") +label when they log in. If you do want to log in via the hacked ssh +at other labels use the attr command to set the smack value on the +home directory and it's contents. + +You can add access rules in /etc/smack/accesses. They take the form: + + subjectlabel objectlabel access + +access is a combination of the letters rwxa which specify the +kind of access permitted a subject with subjectlabel on an +object with objectlabel. If there is no rule no access is allowed. + +A process can see the smack label it is running with by +reading /proc/self/attr/current. A privileged process can +set the process smack by writing there. + +From the Smack Whitepaper: + +The Simplified Mandatory Access Control Kernel + +Casey Schaufler [email protected] + +Mandatory Access Control + +Computer systems employ a variety of schemes to constrain how information is +shared among the people and services using the machine. Some of these schemes +allow the program or user to decide what other programs or users are allowed +access to pieces of data. These schemes are called discretionary access +control mechanisms because the access control is specified at the discretion +of the user. Other schemes do not leave the decision regarding what a user or +program can access up to users or programs. These schemes are called mandatory +access control mechanisms because you don't have a choice regarding the users +or programs that have access to pieces of data. + +Bell & LaPadula + +From the middle of the 1980's until the turn of the century Mandatory Access +Control (MAC) was very closely associated with the Bell & LaPadula security +model, a mathematical description of the United States Department of Defense +policy for marking paper documents. MAC in this form enjoyed a following +within the Capital Beltway and Scandinavian supercomputer centers but was +often sited as failing to address general needs. + +Domain Type Enforcement + +Around the turn of the century Domain Type Enforcement (DTE) became popular. +This scheme organizes users, programs, and data into domains that are +protected from each other. This scheme has been widely deployed as a component +of popular Linux distributions. The administrative overhead required to +maintain this scheme and the detailed understanding of the whole system +necessary to provide a secure domain mapping leads to the scheme being +disabled or used in limited ways in the majority of cases. + +Smack + +Smack is a Mandatory Access Control mechanism designed to provide useful MAC +while avoiding the pitfalls of its predecessors. The limitations of Bell & +LaPadula are addressed by providing a scheme whereby access can be controlled +according to the requirements of the system and its purpose rather than those +imposed by an arcane government policy. The complexity of Domain Type +Enforcement and avoided by defining access controls in terms of the access +modes already in use. + +Smack Terminology + +The jargon used to talk about Smack will be familiar to those who have dealt +with other MAC systems and shouldn't be too difficult for the uninitiated to +pick up. There are four terms that are used in a specific way and that are +especially important: + + Subject: A subject is an active entity on the computer system. + On Smack a subject is a task, which is in turn the basic unit + of execution. + + Object: An object is a passive entity on the computer system. + On Smack files of all types, IPC, and tasks can be objects. + + Access: Any attempt by a subject to put information into or get + information from an object is an access. + + Label: Data that identifies the Mandatory Access Control + characteristics of a subject or an object. + +These definitions are consistent with the traditional use in the security +community. There are also some terms from Linux that are likely to crop up: + + Capability: A task that possesses a capability has permission to + violate an aspect of the system security policy, as identified by + the specific capability. A task that possesses one or more + capabilities is a privileged task, whereas a task with no + capabilities is an unprivileged task. + + Privilege: A task that is allowed to violate the system security + policy is said to have privilege. As of this writing a task can + have privilege either by possessing capabilities or by having an + effective user of root. + +Smack Basics + +Smack is an extension to a Linux system. It enforces additional restrictions +on what subjects can access which objects, based on the labels attached to +each of the subject and the object. + +Labels + +Smack labels are ASCII character strings, one to twenty-three characters in +length. Single character labels using special characters, that being anything +other than a letter or digit, are reserved for use by the Smack development +team. Smack labels are unstructured, case sensitive, and the only operation +ever performed on them is comparison for equality. Smack labels cannot +contain unprintable characters or the "/" (slash) character. + +There are some predefined labels: + + _ Pronounced "floor", a single underscore character. + ^ Pronounced "hat", a single circumflex character. + * Pronounced "star", a single asterisk character. + ? Pronounced "huh", a single question mark character. + +Every task on a Smack system is assigned a label. System tasks, such as +init(8) and systems daemons, are run with the floor ("_") label. User tasks +are assigned labels according to the specification found in the +/etc/smack/user configuration file. + +Access Rules + +Smack uses the traditional access modes of Linux. These modes are read, +execute, write, and occasionally append. There are a few cases where the +access mode may not be obvious. These include: + + Signals: A signal is a write operation from the subject task to + the object task. + Internet Domain IPC: Transmission of a packet is considered a + write operation from the source task to the destination task. + +Smack restricts access based on the label attached to a subject and the label +attached to the object it is trying to access. The rules enforced are, in +order: + + 1. Any access requested by a task labeled "*" is denied. + 2. A read or execute access requested by a task labeled "^" + is permitted. + 3. A read or execute access requested on an object labeled "_" + is permitted. + 4. Any access requested on an object labeled "*" is permitted. + 5. Any access requested by a task on an object with the same + label is permitted. + 6. Any access requested that is explicitly defined in the loaded + rule set is permitted. + 7. Any other access is denied. + +Smack Access Rules + +With the isolation provided by Smack access separation is simple. There are +many interesting cases where limited access by subjects to objects with +different labels is desired. One example is the familiar spy model of +sensitivity, where a scientist working on a highly classified project would be +able to read documents of lower classifications and anything she writes will +be "born" highly classified. To accommodate such schemes Smack includes a +mechanism for specifying rules allowing access between labels. + +Access Rule Format + +The format of an access rule is: + + subject-label object-label access + +Where subject-label is the Smack label of the task, object-label is the Smack +label of the thing being accessed, and access is a string specifying the sort +of access allowed. The Smack labels are limited to 23 characters. The access +specification is searched for letters that describe access modes: + + a: indicates that append access should be granted. + r: indicates that read access should be granted. + w: indicates that write access should be granted. + x: indicates that execute access should be granted. + +Uppercase values for the specification letters are allowed as well. +Access mode specifications can be in any order. Examples of acceptable rules +are: + + TopSecret Secret rx + Secret Unclass R + Manager Game x + User HR w + New Old rRrRr + Closed Off - + +Examples of unacceptable rules are: + + Top Secret Secret rx + Ace Ace r + Odd spells waxbeans + +Spaces are not allowed in labels. Since a subject always has access to files +with the same label specifying a rule for that case is pointless. Only +valid letters (rwxaRWXA) and the dash ('-') character are allowed in +access specifications. The dash is a placeholder, so "a-r" is the same +as "ar". A lone dash is used to specify that no access should be allowed. + +Applying Access Rules + +The developers of Linux rarely define new sorts of things, usually importing +schemes and concepts from other systems. Most often, the other systems are +variants of Unix. Unix has many endearing properties, but consistency of +access control models is not one of them. Smack strives to treat accesses as +uniformly as is sensible while keeping with the spirit of the underlying +mechanism. + +File system objects including files, directories, named pipes, symbolic links, +and devices require access permissions that closely match those used by mode +bit access. To open a file for reading read access is required on the file. To +search a directory requires execute access. Creating a file with write access +requires both read and write access on the containing directory. Deleting a +file requires read and write access to the file and to the containing +directory. It is possible that a user may be able to see that a file exists +but not any of its attributes by the circumstance of having read access to the +containing directory but not to the differently labeled file. This is an +artifact of the file name being data in the directory, not a part of the file. + +IPC objects, message queues, semaphore sets, and memory segments exist in flat +namespaces and access requests are only required to match the object in +question. + +Process objects reflect tasks on the system and the Smack label used to access +them is the same Smack label that the task would use for its own access +attempts. Sending a signal via the kill() system call is a write operation +from the signaler to the recipient. Debugging a process requires both reading +and writing. Creating a new task is an internal operation that results in two +tasks with identical Smack labels and requires no access checks. + +Sockets are data structures attached to processes and sending a packet from +one process to another requires that the sender have write access to the +receiver. The receiver is not required to have read access to the sender. + +Setting Access Rules + +The configuration file /etc/smack/accesses contains the rules to be set at +system startup. The contents are written to the special file /smack/load. +Rules can be written to /smack/load at any time and take effect immediately. +For any pair of subject and object labels there can be only one rule, with the +most recently specified overriding any earlier specification. + +Task Attribute + +The Smack label of a process can be read from /proc/<pid>/attr/current. A +process can read its own Smack label from /proc/self/attr/current. A +privileged process can change its own Smack label by writing to +/proc/self/attr/current but not the label of another process. + +File Attribute + +The Smack label of a filesystem object is stored as an extended attribute +named SMACK64 on the file. This attribute is in the security namespace. It can +only be changed by a process with privilege. + +Privilege + +A process with CAP_MAC_OVERRIDE is privileged. + +Smack Networking + +As mentioned before, Smack enforces access control on network protocol +transmissions. Every packet sent by a Smack process is tagged with its Smack +label. This is done by adding a CIPSO tag to the header of the IP packet. Each +packet received is expected to have a CIPSO tag that identifies the label and +if it lacks such a tag the network ambient label is assumed. Before the packet +is delivered a check is made to determine that a subject with the label on the +packet has write access to the receiving process and if that is not the case +the packet is dropped. + +CIPSO Configuration + +It is normally unnecessary to specify the CIPSO configuration. The default +values used by the system handle all internal cases. Smack will compose CIPSO +label values to match the Smack labels being used without administrative +intervention. Unlabeled packets that come into the system will be given the +ambient label. + +Smack requires configuration in the case where packets from a system that is +not smack that speaks CIPSO may be encountered. Usually this will be a Trusted +Solaris system, but there are other, less widely deployed systems out there. +CIPSO provides 3 important values, a Domain Of Interpretation (DOI), a level, +and a category set with each packet. The DOI is intended to identify a group +of systems that use compatible labeling schemes, and the DOI specified on the +smack system must match that of the remote system or packets will be +discarded. The DOI is 3 by default. The value can be read from /smack/doi and +can be changed by writing to /smack/doi. + +The label and category set are mapped to a Smack label as defined in +/etc/smack/cipso. + +A Smack/CIPSO mapping has the form: + + smack level[/category[,category]*] + +Smack does not expect the level or category sets to be related in any +particular way and does not assume or assign accesses based on them. Some +examples of mappings: + + TopSecret 7 + TS:A,B 7/1,2 + SecBDE 5/2,4,6 + RAFTERS 7/12,26 + +The ":" and "," characters are permitted in a Smack label but have no special +meaning. + +The mapping of Smack labels to CIPSO values is defined by writing to +/smack/cipso. + +In addition to explicit mappings Smack supports direct CIPSO mappings. One +CIPSO level is used to indicate that the category set passed in the packet is +in fact an encoding of the Smack label. The level used is 250 by default. The +value can be read from /smack/direct and changed by writing to /smack/direct. + +Socket Attributes + +There are two attributes that are associated with sockets. These attributes +can only be set by privileged tasks, but any task can read them for their own +sockets. + + SMACK64IPIN: The Smack label of the task object. A privileged + program that will enforce policy may set this to the star label. + + SMACK64IPOUT: The Smack label transmitted with outgoing packets. + A privileged program may set this to match the label of another + task with which it hopes to communicate. + +Writing Applications for Smack + +There are three sorts of applications that will run on a Smack system. How an +application interacts with Smack will determine what it will have to do to +work properly under Smack. + +Smack Ignorant Applications + +By far the majority of applications have no reason whatever to care about the +unique properties of Smack. Since invoking a program has no impact on the +Smack label associated with the process the only concern likely to arise is +whether the process has execute access to the program. + +Smack Relevant Applications + +Some programs can be improved by teaching them about Smack, but do not make +any security decisions themselves. The utility ls(1) is one example of such a +program. + +Smack Enforcing Applications + +These are special programs that not only know about Smack, but participate in +the enforcement of system policy. In most cases these are the programs that +set up user sessions. There are also network services that provide information +to processes running with various labels. + +File System Interfaces + +Smack maintains labels on file system objects using extended attributes. The +Smack label of a file, directory, or other file system object can be obtained +using getxattr(2). + + len = getxattr("/", "security.SMACK64", value, sizeof (value)); + +will put the Smack label of the root directory into value. A privileged +process can set the Smack label of a file system object with setxattr(2). + + len = strlen("Rubble"); + rc = setxattr("/foo", "security.SMACK64", "Rubble", len, 0); + +will set the Smack label of /foo to "Rubble" if the program has appropriate +privilege. + +Socket Interfaces + +The socket attributes can be read using fgetxattr(2). + +A privileged process can set the Smack label of outgoing packets with +fsetxattr(2). + + len = strlen("Rubble"); + rc = fsetxattr(fd, "security.SMACK64IPOUT", "Rubble", len, 0); + +will set the Smack label "Rubble" on packets going out from the socket if the +program has appropriate privilege. + + rc = fsetxattr(fd, "security.SMACK64IPIN, "*", strlen("*"), 0); + +will set the Smack label "*" as the object label against which incoming +packets will be checked if the program has appropriate privilege. + +Administration + +Smack supports some mount options: + + smackfsdef=label: specifies the label to give files that lack + the Smack label extended attribute. + + smackfsroot=label: specifies the label to assign the root of the + file system if it lacks the Smack extended attribute. + + smackfshat=label: specifies a label that must have read access to + all labels set on the filesystem. Not yet enforced. + + smackfsfloor=label: specifies a label to which all labels set on the + filesystem must have read access. Not yet enforced. + +These mount options apply to all file system types. +
security/Kconfig +1 -0
@@ -104,6 +104,7 @@ config SECURITY_ROOTPLUG If you are unsure how to answer this question, answer N. source security/selinux/Kconfig +source security/smack/Kconfig endmenu
security/Makefile +2 -0
@@ -4,6 +4,7 @@ obj-$(CONFIG_KEYS) += keys/ subdir-$(CONFIG_SECURITY_SELINUX) += selinux +subdir-$(CONFIG_SECURITY_SMACK) += smack # if we don't select a security model, use the default capabilities ifneq ($(CONFIG_SECURITY),y)
@@ -14,5 +15,6 @@ endif obj-$(CONFIG_SECURITY) += security.o dummy.o inode.o # Must precede capability.o in order to stack properly. obj-$(CONFIG_SECURITY_SELINUX) += selinux/built-in.o +obj-$(CONFIG_SECURITY_SMACK) += commoncap.o smack/built-in.o obj-$(CONFIG_SECURITY_CAPABILITIES) += commoncap.o capability.o obj-$(CONFIG_SECURITY_ROOTPLUG) += commoncap.o root_plug.o
security/smack/Kconfig +10 -0
@@ -0,0 +1,10 @@ +config SECURITY_SMACK + bool "Simplified Mandatory Access Control Kernel Support" + depends on NETLABEL && SECURITY_NETWORK + default n + help + This selects the Simplified Mandatory Access Control Kernel. + Smack is useful for sensitivity, integrity, and a variety + of other mandatory security schemes. + If you are unsure how to answer this question, answer N. +
security/smack/Makefile +7 -0
@@ -0,0 +1,7 @@ +# +# Makefile for the SMACK LSM +# + +obj-$(CONFIG_SECURITY_SMACK) := smack.o + +smack-y := smack_lsm.o smack_access.o smackfs.o
security/smack/smack_access.c +353 -0
353 lines not on this page · raw message
security/smack/smackfs.c +932 -0
932 lines not on this page · raw message
security/smack/smack.h +222 -0
222 lines not on this page · raw message
security/smack/smack_lsm.c +2529 -0
978 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
PATCH 1/1 Smackv10: Smack rules grammar + their stateful parser
Quoted 4 lines · Casey Schaufler
Still to come:
- Final cleanup of
smack_load_writeandsmack_cipso_write.
Hi All,
After agreeing with Casey on the "load" input grammar yesterday, here's the final grammar and its parser (which needs more testing):
A Smack Rule in an "egrep" format is:
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
where Subject/Object strings are in the form:
"^[^/[:space:][:cntrl:]]{1,SMK_MAXLEN}$"
---
Bashv3 builtin "echo" behaves very strangely to -EINVAL. It sends all the buffers that causes -EINVAL again in subsequent echo invocations.
i.e.
echo "Invalid Rule" > /smack/load # -EINVAL returned echo "Valid Rule" > /smack/load
In seconod iteration, echo sends the first invalid buffer again then sends the new one. This causes a
"Invalid Rule\nValid Rule" buffer sent to write().
IMHO, this is a bug in builtin echo. The external /bin/echo doesn't cause such strange behaviour.
security/smack/smack.h +12 -0
@@ -219,4 +219,16 @@ static inline char *smk_of_inode(const struct inode *isp) return sip->smk_inode; } +static inline int isblank (char c) +{ +return (c ==' ' || c =='\t' ); +} + +#define swap (x, y, type) \ +do { \ + type tmp = x; \ + x = y; \ + y = tmp; \ +} while (0); \ +#endif /* _SECURITY_SMACK_H */
security/smack/smackfs.c +167 -32
267 lines not on this page · raw message
--
Ahmed S. Darwish
Homepage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 9 lines · Ahmed S. Darwish
Bashv3 builtin "echo" behaves very strangely to
-EINVAL. It sends all the buffers that causes-EINVALagain in subsequent echo invocations.i.e.
echo "Invalid Rule" > /smack/load #-EINVALreturned echo "Valid Rule" > /smack/loadIn seconod iteration, echo sends the first invalid buffer again then sends the new one. This causes a "Invalid Rule\nValid Rule" buffer sent to
write().IMHO, this is a bug in builtin echo. The external
/bin/echodoesn't cause such strange behaviour.
Actually, what causes problems here is something between a bug and a feature in libc's buffering. Basically the -EINVAL error causes libc to leave its data in the file-output buffer despite the file being closed and reopened. Since a standalone echo just exits that buffer is discarded, but for the bash builtin it hangs around in the buffer for a while and ends up getting prepended to the following echo statement. There's actually multiple ways to make this fail; this is just the simplest.
Cheers,
Kyle Moffett
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 16 lines · Kyle Moffett
Quoted 8 lines · Ahmed S. Darwish
Bashv3 builtin "echo" behaves very strangely to
-EINVAL. It sends all the buffers that causes-EINVALagain in subsequent echo invocations.i.e.
echo "Invalid Rule" > /smack/load #-EINVALreturned echo "Valid Rule" > /smack/loadQuoted 6 lines
In seconod iteration, echo sends the first invalid buffer again then sends the new one. This causes a "Invalid Rule\nValid Rule" buffer sent to
write().IMHO, this is a bug in builtin echo. The external
/bin/echodoesn't cause such strange behaviour.
Quoted 9 lines
Actually, what causes problems here is something between a bug and a feature in libc's buffering. Basically the
-EINVALerror causes libc to leave its data in the file-output buffer despite the file being closed and reopened. Since a standalone echo just exits that buffer is discarded, but for the bash builtin it hangs around in the buffer for a while and ends up getting prepended to the following echo statement. There's actually multiple ways to make this fail; this is just the simplest.
Thanks a lot for such a useful info. Is there a way from my side to make subsequent echo invocations not affected by previous failed ones ?
Regards,
Signature · 3 lines
Ahmed S. Darwish Homepage: http://darwish.07.googlepages.com Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Ahmed S. DarwishHi!
Quoted 9 lines
Quoted 3 lines
Still to come:
- Final cleanup of
smack_load_writeandsmack_cipso_write.Hi All,
After agreeing with Casey on the "load" input grammar yesterday, here's the final grammar and its parser (which needs more testing):
Quoted 7 lines
A Smack Rule in an "egrep" format is:
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
where Subject/Object strings are in the form:
"^[^/[:space:][:cntrl:]]{1,SMK_MAXLEN}$"
Can we avoid string parsers in the kernel?
security/smack/smack.h static inline char *smk_of_inode(const struct inode *isp) · line 225
Quoted 4 lines
security/smack/smack.h +4
-0+static inline int isblank (char c) +{ +return (c ==' ' || c =='\t' ); +}
This sounds like enough for 'NAK'.
Pavel,
who still thinks smack rules should be parsed in userspace and compiled into selinux rules...
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 13 lines · Pavel Machek
Hi!
Quoted 11 lines
Quoted 3 lines
Still to come:
- Final cleanup of
smack_load_writeandsmack_cipso_write.Hi All,
After agreeing with Casey on the "load" input grammar yesterday, here's the final grammar and its parser (which needs more testing):
A Smack Rule in an "egrep" format is:
Quoted 8 lines
Quoted 5 lines
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
where Subject/Object strings are in the form:
"^[^/[:space:][:cntrl:]]{1,SMK_MAXLEN}$"
Can we avoid string parsers in the kernel?
I've suggested that at first, but (hoping not to misquote Al) Al viro said that the parsing is simple enough and no need exists for a user-space utility.
Quoted 8 lines
security/smack/smack.h static inline char *smk_of_inode(const struct inode *isp) · line 225
Quoted 5 lines
security/smack/smack.h +4
-0+static inline int isblank (char c) +{ +return (c ==' ' || c =='\t' ); +}This sounds like enough for 'NAK'.
Would you please show the reason for the NAK so I can modify the code ?
Thank you,
Quoted 4 lines
Pavel,
who still thinks smack rules should be parsed in userspace and compiled into selinux rules...
Signature · 3 lines
Ahmed S. Darwish Homepage: http://darwish.07.googlepages.com Blog: http://darwish-07.blogspot.com
Casey Schaufler
maintainerRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 14 lines · --- "Ahmed S. Darwish
Quoted 13 lines · Pavel Machek
Hi!
Quoted 11 lines
Quoted 3 lines
Still to come:
- Final cleanup of
smack_load_writeandsmack_cipso_write.Hi All,
After agreeing with Casey on the "load" input grammar yesterday, here's the final grammar and its parser (which needs more testing):
A Smack Rule in an "egrep" format is:
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
where Subject/Object strings are in the form:
"^[^/[:space:][:cntrl:]]{1,SMK_MAXLEN}$"
Can we avoid string parsers in the kernel?
Woof. We've devolved into Al saying it must be done this way, and Pavel saying it can't be done this way.
That's OK. Maybe it's time that I stepped back and came up with a twisted mechanism for achieving my ends, as the obvious mechanism appears as if it will always call down disapproval.
Quoted 13 lines
I've suggested that at first, but (hoping not to misquote Al) Al viro said that the parsing is simple enough and no need exists for a user-space utility.
Quoted 8 lines
security/smack/smack.h static inline char *smk_of_inode(const struct inode *isp) · line 225
Quoted 5 lines
security/smack/smack.h +4
-0+static inline int isblank (char c) +{ +return (c ==' ' || c =='\t' ); +}This sounds like enough for 'NAK'.
Quoted 1 line
Would you please show the reason for the NAK so I can modify the code ?
yeah, my head needs scratching after that one.
Quoted 6 lines
Thank you,
Quoted 3 lines
Pavel,
who still thinks smack rules should be parsed in userspace and compiled into selinux rules...
Casey, who still thinks Pavel doesn't get it.
Casey Schaufler
[email protected]
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Pavel MachekQuoted 13 lines · Pavel Machek
Hi!
Quoted 11 lines
Quoted 3 lines
Still to come:
- Final cleanup of
smack_load_writeandsmack_cipso_write.Hi All,
After agreeing with Casey on the "load" input grammar yesterday, here's the final grammar and its parser (which needs more testing):
A Smack Rule in an "egrep" format is:
Quoted 8 lines
Quoted 5 lines
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
where Subject/Object strings are in the form:
"^[^/[:space:][:cntrl:]]{1,SMK_MAXLEN}$"
Can we avoid string parsers in the kernel?
Ok, Could someone suggest a better idea please ?.
I thought about packing the rules in a structure and sending it over an ioctl() command. Is this applicable ?
Quoted 15 lines
security/smack/smack.h static inline char *smk_of_inode(const struct inode *isp) · line 225
Quoted 5 lines
security/smack/smack.h +4
-0+static inline int isblank (char c) +{ +return (c ==' ' || c =='\t' ); +}This sounds like enough for 'NAK'.
Pavel,
who still thinks smack rules should be parsed in userspace and compiled into selinux rules...Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 6 lines · Ahmed S. Darwish
Quoted 3 lines · Pavel Machek
Can we avoid string parsers in the kernel?
Ok, Could someone suggest a better idea please ?.
I personally think string parsers are *much* better than the alternatives (which basically boil down to nasty binary interfaces)
Quoted 2 lines
I thought about packing the rules in a structure and sending it over an
ioctl()command. Is this applicable ?
That's *MUCH* worse.
Strings are nice. They aren't that complex, and as long as it's not a performance-critical area, there are basically no downsides.
Binary structures and ioctl's are *much* worse. They are totally undebuggable with generic tools (think "echo" or "strace"), and they are a total nightmare to parse across architectures and pointer sizes.
So the rule should be: always use strings if at all possible and relevant. If the data is fundamentally binary, it shouldn't be re-coded to ascii (no real advantage), but if the data is "stringish", and there aren't big performance issues, then keep it as strings.
Linus
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 2 lines · Linus Torvalds
I personally think string parsers are *much* better than the alternatives (which basically boil down to nasty binary interfaces)
Me too.
Quoted 3 lines
Binary structures and ioctl's are *much* worse. They are totally undebuggable with generic tools (think "echo" or "strace"), and they are a total nightmare to parse across architectures and pointer sizes.
Not only pointer sizes bugs, but also checking pointer address costs. For binary policy, we have to examine ->next pointer is valid or not. We can't blindly use address supplied from userland.
I have encountered mismatch of kernel version and AppArmor's policy parser version when I just updated only kernel. As a result, the segmentation faults rushed toward me.
From this experience, TOMOYO still uses string parser in the kernel.
If a parser doesn't consume much stack (i.e. call functions recursively), I think it is no problem.
Thanks.
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 9 lines · Tetsuo Handa
Quoted 2 lines · Linus Torvalds
I personally think string parsers are *much* better than the alternatives (which basically boil down to nasty binary interfaces)
Me too.
Quoted 3 lines
Binary structures and ioctl's are *much* worse. They are totally undebuggable with generic tools (think "echo" or "strace"), and they are a total nightmare to parse across architectures and pointer sizes.
Quoted 8 lines
Not only pointer sizes bugs, but also checking pointer address costs. For binary policy, we have to examine ->next pointer is valid or not. We can't blindly use address supplied from userland.
I have encountered mismatch of kernel version and AppArmor's policy parser version when I just updated only kernel. As a result, the segmentation faults rushed toward me.
From this experience, TOMOYO still uses string parser in the kernel.
If a parser doesn't consume much stack (i.e. call functions recursively), I think it is no problem.
You have a "\?" pattern which is defined as "1 byte character other than '/'".
The user usually doesn't know how many bytes a character in a path or file name on his system has.
IMHO not ideal for name-based MAC...
Quoted 1 line
Thanks.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Hello.
Quoted 2 lines · Adrian Bunk
You have a "\?" pattern which is defined as "1 byte character other than '/'".
Don't worry. The "\?" pattern is for temporary files with /tmp/prefixXXXXXX pattern. /tmp/prefixXXXXXX is represented as /tmp/prefix\?\?\?\?\?\? in TOMOYO Linux's syntax.
Quoted 2 lines
The user usually doesn't know how many bytes a character in a path or file name on his system has.
The "\*" pattern is for this purpose which means more than 0 byte characters other than '/'.
TOMOYO supports various patterns
http://tomoyo.sourceforge.jp/en/1.5.x/policy-reference.html#exception_policy.conf
TOMOYO Linux handles string using 7bit ASCII. In TOMOYO Linux, a byte 0x21 <= c <= 0x7E && c != 0x5C is represented as is, c == 0x5C is represented as \\,0x01 <= c <= 0x20 || 0x7F <= c <= 0xFF is represented as \ooo style. c == 0x00 is not needed since it is used as end-of-string marker. This rule makes any string passed from/to kernel safely.
Thanks.
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 8 lines · Tetsuo Handa
Hello.
Quoted 2 lines · Adrian Bunk
You have a "\?" pattern which is defined as "1 byte character other than '/'".
Don't worry. The "\?" pattern is for temporary files with
/tmp/prefixXXXXXXpattern./tmp/prefixXXXXXXis represented as /tmp/prefix\?\?\?\?\?\? in TOMOYO Linux's syntax.
Quoted 6 lines
Quoted 2 lines
The user usually doesn't know how many bytes a character in a path or file name on his system has.
The "\*" pattern is for this purpose which means more than 0 byte characters other than '/'.
TOMOYO supports various patterns
http://tomoyo.sourceforge.jp/en/1.5.x/policy-reference.html#exception_policy.conf
And there you document \* as well as \? as wildcards for "pathname patterns".
And \* is not a replacement for \?. It's quite common to have both ways to express "one character" and to express "at least one character", and both have their use cases and will get used if available.
Quoted 6 lines
TOMOYO Linux handles string using 7bit ASCII. In TOMOYO Linux, a byte
0x21<= c <=0x7E&& c !=0x5Cis represented as is, c ==0x5Cis represented as \\,0x01<= c <=0x20||0x7F<= c <=0xFFis represented as \ooo style. c ==0x00is not needed since it is used as end-of-string marker. This rule makes any string passed from/to kernel safely.
That's unrelated to this problem.
You talk about your internal byte representation.
But the problem is that in your code you only match one byte for \?, and this might or might not be equal to one character.
Quoted 1 line
Thanks.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Hello.
Quoted 3 lines · Adrian Bunk
And \* is not a replacement for \?. It's quite common to have both ways to express "one character" and to express "at least one character", and both have their use cases and will get used if available.
You can use \? to represent "one character" and
\?\* to represent "at least one character".
Quoted 2 lines
But the problem is that in your code you only match one byte for \?, and this might or might not be equal to one character.
"one byte" is almost equal to "one character".
"\?" matches to one of the following types.
* 1 ASCII printable character (for 0x21-0x2E or 0x30-0x5B or 0x5D-0x7E) * 2 ASCII printable characters \\ (for 0x5C, which means single "\") * 4 ASCII printable characters \ooo (for 0x01-0x20 or 0x7F-0xFF, where "ooo" is octal value)
These 3 types represents one *byte*.
I want to say "\? matches to one character",
but since expression of a character depends on the value of that byte, I'm saying "\? matches to one *byte* character" instead.
Well, this sentence might be confusing, but how can I express more accurately?
Thanks.
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 8 lines · Tetsuo Handa
Hello.
Quoted 3 lines · Adrian Bunk
And \* is not a replacement for \?. It's quite common to have both ways to express "one character" and to express "at least one character", and both have their use cases and will get used if available.
You can use \? to represent "one character" and
\?\* to represent "at least one character".
Not "one character", it's "one byte".
Quoted 9 lines
Quoted 2 lines
But the problem is that in your code you only match one byte for \?, and this might or might not be equal to one character.
"one byte" is almost equal to "one character".
"\?" matches to one of the following types.* 1 ASCII printable character (for 0x21-0x2E or 0x30-0x5B or 0x5D-0x7E) * 2 ASCII printable characters \\ (for 0x5C, which means single "\") * 4 ASCII printable characters \ooo (for 0x01-0x20 or 0x7F-0xFF, where "ooo" is octal value)
Quoted 5 lines
These 3 types represents one *byte*.
I want to say "\? matches to one character",
but since expression of a character depends on the value of that byte, I'm saying "\? matches to one *byte* character" instead.
Well, this sentence might be confusing, but how can I express more accurately?
The problem is that your code matches one byte, not one character.
More or less all userspace programs handle multi-byte UTF-8 characters just fine without bothering the user with the fact whether a character consists of one or more bytes.
And users will try to use this \? for matching one character when writing a pattern that denies access.
That's not just a documentation problem.
Quoted 1 line
Thanks.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Hello.
Quoted 5 lines · Adrian Bunk
The problem is that your code matches one byte, not one character.
More or less all userspace programs handle multi-byte UTF-8 characters just fine without bothering the user with the fact whether a character consists of one or more bytes.
I understood what you are saying.
You are saying "a character" does not always consist of one byte, while I'm saying "a character" does always consist of one byte.
Yes, some userspace programs don't use strcmp()
since strcmp() can't handle some encodings like UTF-16. But the kernel uses strcmp()
since the VFS related functions can't handle encodings which contains '\0' in the pathname.
VFS related functions assume that '\0' is end-of-string marker.
So, from the point of view of userland programs,
'\?' should match to a single character (which depends on encoding). But from the point of view of the kernel,
'\?' should match to a single byte (which doesn't depend on encoding). Handling all possible encoding in the kernel is too difficult to implement. I'll continue using '\?' matches to a single byte.
Quoted 2 lines
And users will try to use this \? for matching one character when writing a pattern that denies access.
Yes, but since this string is handled by the *kernel*, I want users follow point of view of the kernel.
Thanks.
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 13 lines · Tetsuo Handa
Hello.
Quoted 5 lines · Adrian Bunk
The problem is that your code matches one byte, not one character.
More or less all userspace programs handle multi-byte UTF-8 characters just fine without bothering the user with the fact whether a character consists of one or more bytes.
I understood what you are saying.
You are saying "a character" does not always consist of one byte, while I'm saying "a character" does always consist of one byte.
Quoted 7 lines
Yes, some userspace programs don't use
strcmp()
sincestrcmp()can't handle some encodings like UTF-16. But the kernel usesstrcmp()
since the VFS related functions can't handle encodings which contains '\0' in the pathname.
VFS related functions assume that '\0' is end-of-string marker.
...
The common case isn't UTF-16, it's UTF-8.
And UTF-8 is both quite common and doesn't have this problem with '\0'.
Quoted 4 lines
Quoted 2 lines
And users will try to use this \? for matching one character when writing a pattern that denies access.
Yes, but since this string is handled by the *kernel*, I want users follow point of view of the kernel.
Users are used to deal with characters and hot having to bother with all the mess of different encodings.
Having patterns that describe rules to deny access in an LSM breaking this expectation is really a bad thing.
Quoted 1 line
Thanks.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Linus TorvaldsQuoted 22 lines · Linus Torvalds
Quoted 6 lines · Ahmed S. Darwish
Quoted 3 lines · Pavel Machek
Can we avoid string parsers in the kernel?
Ok, Could someone suggest a better idea please ?.
I personally think string parsers are *much* better than the alternatives (which basically boil down to nasty binary interfaces)
Quoted 2 lines
I thought about packing the rules in a structure and sending it over an
ioctl()command. Is this applicable ?That's *MUCH* worse.
Strings are nice. They aren't that complex, and as long as it's not a performance-critical area, there are basically no downsides.
Quoted 9 lines
Binary structures and ioctl's are *much* worse. They are totally undebuggable with generic tools (think "echo" or "strace"), and they are a total nightmare to parse across architectures and pointer sizes.
So the rule should be: always use strings if at all possible and relevant. If the data is fundamentally binary, it shouldn't be re-coded to ascii (no real advantage), but if the data is "stringish", and there aren't big performance issues, then keep it as strings.
Thanks a lot for such a kind advice. I'll keep that in my mind.
Regards,
Signature · 3 lines
Ahmed S. Darwish Homepage: http://darwish.07.googlepages.com Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Linus TorvaldsQuoted 6 lines · Linus Torvalds
Quoted 2 lines · Ahmed S. Darwish
Quoted 1 line · Pavel Machek
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Can we avoid string parsers in the kernel?
Ok, Could someone suggest a better idea please ?.
I personally think string parsers are *much* better than the alternatives (which basically boil down to nasty binary interfaces)
...
How do we get the information about the character encoding of the string to be parsed into the kernel?
Quoted 1 line
Linus
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 3 lines · Adrian Bunk
How do we get the information about the character encoding of the string to be parsed into the kernel?
I really would expect that kernel strings don't have an encoding. They're just C strings: a NUL-terminated stream of bytes.
Linus
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 9 lines · Linus Torvalds
Quoted 3 lines · Adrian Bunk
How do we get the information about the character encoding of the string to be parsed into the kernel?
I really would expect that kernel strings don't have an encoding. They're just C strings: a NUL-terminated stream of bytes.
You were the one who suggested to _parse_ strings in the kernel.
Some basic parsing is possible due to the fact that all encodings you usually get are extensions of ASCII with additional properties.
But in this thread we discussed that the following implemented things in Smack and TOMOYO currently do not work correctly when the string is encoded in UTF-8:
- isgraph()
- isspace()
- match one character
How do you suggest to implement them in a safe way without knowing about the encoding?
Quoted 1 line
Linus
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 2 lines · Adrian Bunk
You were the one who suggested to _parse_ strings in the kernel.
So?
We do that for lots of things.
What do you think a filename is? And yes, we parse it. Things like '/' and '.' and '..' have magic meaning.
You don't need to bring up idiotic things like character sets. You can see it as a byte string. You're done with it.
Linus
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 8 lines · Linus Torvalds
Quoted 2 lines · Adrian Bunk
You were the one who suggested to _parse_ strings in the kernel.
So?
Quoted 7 lines
We do that for lots of things.
What do you think a filename is? And yes, we parse it. Things like '/' and '.' and '..' have magic meaning.
You don't need to bring up idiotic things like character sets. You can see it as a byte string. You're done with it.
We have the following properties in the character sets we handle:
- every ASCII character is encoded with the same byte as in ASCII
- if the eighth bit is 0, the byte can't be part of a multi-byte character
- no ASCII character can be encoded in a different way
This (plus most likely some other properties I've missed to mention) allows some parsing based on ASCII characters.
But if you want to match "one character" (like TOMOYO does) or want to check for printable characters except space (like Smack does) you must know whether the byte string 0xC3 0xA0 is the character à or a sequence of two characters with the second one being NBSP.
Quoted 1 line
Linus
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 5 lines · Adrian Bunk
But if you want to match "one character" (like TOMOYO does) or want to check for printable characters except space (like Smack does) you must know whether the byte string
0xC30xA0is the character à or a sequence of two characters with the second one being NBSP.
No you don't. You just check the binary value, and decide that ' ', '\t' and '\n' are special.
Don't go get excited.
Linus
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Adrian Bunk
But if you want to match "one character" (like TOMOYO does) or want to check for printable characters except space (like Smack does) you must know whether the byte string
0xC30xA0is the character à or a sequence of two characters with the second one being NBSP.
Quoted 4 lines
No you don't. You just check the binary value, and decide that ' ', '\t' and '\n' are special.
Don't go get excited.
How should TOMOYO implement it's "match one character" in a pattern (used to allow or deny access in a name-based MAC)?
Quoted 1 line
Linus
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Hello.
Quoted 2 lines · Adrian Bunk
How should TOMOYO implement it's "match one character" in a pattern (used to allow or deny access in a name-based MAC)?
TOMOYO assumes that "one character" equals to "one byte" since TOMOYO doesn't consider encoding.
According to your definition of "character" (i.e. it may be multibyte), TOMOYO doesn't implement "match one character" pattern. TOMOYO implements "match one byte" pattern.
Thanks.
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Adrian BunkQuoted 3 lines · Adrian Bunk
How should TOMOYO implement it's "match one character" in a pattern (used to allow or deny access in a name-based MAC)?
.. I think such a design is fundamentally bogus. You don't have "characters". You have "bytes".
So you either implement "match one byte", or you go crazy. It's that simple.
Linus
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 9 lines · Linus Torvalds
Quoted 3 lines · Adrian Bunk
How should TOMOYO implement it's "match one character" in a pattern (used to allow or deny access in a name-based MAC)?
.. I think such a design is fundamentally bogus. You don't have "characters". You have "bytes".
Users are used to work on characters, not on bytes.
Quoted 2 lines
So you either implement "match one byte", or you go crazy. It's that simple.
Sure, you can limit what is possible and what not.
But there are still many pitfalls, e.g. if someone would allow the construct "[abc]" in patterns for matching one of these characters you'd have to ensure that your syntax contains explicit character delimiters or a pattern might match something completely different from what was intended.
My opinion is that extended parsing of non-ASCII strings will cause too many problems, but it seems we can only agree to disagree on this.
Quoted 1 line
Linus
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 2 lines · Adrian Bunk
Users are used to work on characters, not on bytes.
Adrian, stop this idiocy. I'm not interested in listening to your soliloqui about irrelevant stuff.
The kernel works on bytes. Deal with it. Stop whining.
You've been told several times that all the examples you showed were irrelevant, and tomoyo worked on bytes too.
You have been told several times that the VFS layer works on bytes, and has done so since day 1.
You have *also* been told that there is no real other option ("you can work with bytes, or you can go mad"). The normal kernel interfaces have to be locale-independent (parly because it doesn't even KNOW the locale, partly because locale is just totally irrelevant).
And your statement above is a TOTAL AND UTTER LIE.
More people are used to work with bytes (the C language calls them "char") than with what _you_ call "characters". The fact is, people are very very very used to working with 8-bit bytes, and there are a lot more people who understand them than people who understand UTF-8 (never mind any of the other million possible stupid and insane locales).
So can you stop your inane whining now? You can either:
- accept that the kernel works on bytes (*) and that when we talk about parsing strings, we're talking the very _traditional_ C meaning, which is locale-independent, because locales DO NOT WORK in the kernel!
- or you can continue your irrelevant ranting that has nothing to do with anything, but please don't cc me any more. People already pointed out to you that your assumption that "character" means something else than "byte" was wrong.
Please stop this. The absolute *last* thing you want is a kernel that cares about locales. You *also* don't want a kernel that enforces some idiotic UTF-8 rules, since not everybody is using UTF-8. That way lies madness, not to mention totally unnecessary complexity.
Linus
(*) With some *very* rare special cases, notably in the console driver, and for filesystems that are forced by idiot designers to be compatible with crap like OS X and Windows that think that filesystems should be case-insensitive, which is a fundamental problem exactly because of its dependence on locales)
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Adrian BunkQuoted 1 line
Users are used to work on characters, not on bytes.
Absolutely true - but completely missing the point.
When I open a UTF-8 file name as displayed by nautilus the kernel does byte comparisons. The kernel doesn't care what character set is in use.
Quoted 4 lines
Quoted 2 lines
So you either implement "match one byte", or you go crazy. It's that simple.
Sure, you can limit what is possible and what not.
If your regexps are reasonably complete you just have to turn the unicode match into a byte match. That is a user interface problem - in user space.
Alan
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Ahmed S. DarwishQuoted 5 lines · Ahmed S. Darwish
static int
smk_open_load(struct inode *inode, struct file *file){security/smack/smackfs.c +2
-1-return seq_open (file, &load_seq_ops); +if ((file->f_flags &O_ACCMODE ) ==O_RDONLY ) +return seq_open (file, &load_seq_ops);
security/smack/smackfs.c static struct seq_operations load_seq_ops = { · line 185
Quoted 10 lines
security/smack/smackfs.c +10
-0+ +if (down_interruptible (&smack_write_sem)) +return -ERESTARTSYS ; + + load_state =kzalloc (sizeof (struct smack_load_state ),GFP_KERNEL ); +if (!load_state) +return -ENOMEM ; + +return 0 ; +}
Is it right to do the kzalloc with the semaphore being held? Will the lock be held forever If kzalloc failed and -ENOMEM was returned ?
Thanks,
Signature · 3 lines
Ahmed S. Darwish Homepage: http://darwish.07.googlepages.com Blog: http://darwish-07.blogspot.com
PATCH 1/1 Smackv10: Smack rules grammar + their stateful parser(2)
In reply to Ahmed S. DarwishQuoted 13 lines · Ahmed S. Darwish
Quoted 4 lines · Casey Schaufler
Still to come:
- Final cleanup of
smack_load_writeandsmack_cipso_write.Hi All,
After agreeing with Casey on the "load" input grammar yesterday, here's the final grammar and its parser (which needs more testing):
A Smack Rule in an "egrep" format is:
Quoted 9 lines
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
where Subject/Object strings are in the form:
"^[^/[:space:][:cntrl:]]{1,SMK_MAXLEN}$"
Signed-off-by Ahmed S. Darwish <[email protected]>---
Same parser with adhering Casey's concers about previous one and with using the FSM states in a more readable way.
I've double-checked the code for any possible off-by-one/overflow errors. Could someone overcheck this for any possible hidden security holes. Al, please :) ?
security/smack/smackfs.c +176 -32
@@ -25,6 +25,7 @@#include <net/netlabel.h> #include <net/cipso_ipv4.h> #include <linux/seq_file.h> +#include <linux/ctype.h> #include "smack.h" /*
@@ -67,6 +68,47 @@ struct smk_list_entry *smack_list;#define SEQ_READ_FINISHED 1/* +* Disable concurrent writing open() operations +*/ +static struct semaphore smack_write_sem; + +/* +* States for parsing /smack/load rules +*/ +enum load_state { + bol =0 ,/* At Beginning Of Line */ + subject =1 ,/* At a "subject" token */ + object =2 ,/* At an "object" token */ + access =3 ,/* At an "access" token */ + newline =4 ,/* At end Of Line */ + blank =5 ,/* At a space or tab */ +}; + +/* +* Represent current parsing state of /smack/load. Struct +* also stores data needed between an open-release session's +* multiple write() calls +*/ +static struct smack_load_state { +enum load_state state;/* Current FSM parsing state */ +enum load_state prevstate;/* Previous FSM state */ +enum load_state prevtoken;/* Handle state = prevstate = blank */ +struct smack_rule rule;/* Rule to be loaded */ +int label_len;/* Subject/Object labels length so far */ +char subject[SMK_LABELLEN ];/* Subject label */ +char object[SMK_LABELLEN ];/* Object label */ +} *load_state; + +/* +* Rule's tokens separators are spaces and tabs only +*/ +static inline int isblank (char c) +{ +return (c ==' ' || c =='\t' ); +} + + +/* * Seq_file read operations for /smack/load */
@@ -131,12 +173,43 @@ static struct seq_operations load_seq_ops = {* @inode: inode structure representing file * @file: "load" file pointer * -* Connect our load_seq_* operations with /smack/load -* file_operations +* For reading, use load_seq_* seq_file reading operations. +* For writing, prepare a load_state struct to parse +* incoming rules. */ static int smk_open_load (struct inode *inode,struct file *file) { -return seq_open (file, &load_seq_ops); +if ((file->f_flags &O_ACCMODE ) ==O_RDONLY ) +return seq_open (file, &load_seq_ops); + +if (down_interruptible (&smack_write_sem)) +return -ERESTARTSYS ; + + load_state =kzalloc (sizeof (struct smack_load_state ),GFP_KERNEL ); +if (!load_state) +return -ENOMEM ; + +return 0 ; +} + +/** +* smk_release_load - release() for /smack/load +* @inode: inode structure representing file +* @file: "load" file pointer +* +* For a reading session, use the seq_file release +* implementation. +* Otherwise, we are at the end of a writing session so +* clean everything up. +*/ +static int smk_release_load (struct inode *inode,struct file *file) +{ +if ((file->f_flags &O_ACCMODE ) ==O_RDONLY ) +return seq_release (inode, file); + +kfree (load_state); +up (&smack_write_sem); +return 0 ; }/**
@@ -174,7 +247,6 @@ static void smk_set_access(struct smack_rule *srp)return ; } -/** * smk_write_load - write() for /smack/load * @filp: file pointer, not actually used
@@ -182,19 +254,26 @@ static void smk_set_access(struct smack_rule *srp)* @count: bytes sent * @ppos: where to start * -* Returns number of bytes written or error code, as appropriate +* Parse smack rules in below extended regex format: +* "^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*$" +* Where Subject/Object are: "^[^/[:space:][:cntrl:]]{1,SMK_MAXLEN}$" +* +* Handle defragmented rules over several write calls using the +* load_state structure. */ static ssize_t smk_write_load (struct file *file,const char __user *buf,size_t count,loff_t *ppos) { -struct smack_rule rule; -ssize_t rc = count; +struct smack_rule *rule = &load_state->rule; +enum load_state *state = &load_state->state; +enum load_state *prevstate = &load_state->prevstate; +enum load_state *prevtoken = &load_state->prevtoken; +int *label_len = &load_state->label_len; +char *subjectstr = load_state->subject; +char *objectstr = load_state->object; +ssize_t rc = -EINVAL ;char *data =NULL ; -char subjectstr[SMK_LABELLEN ]; -char objectstr[SMK_LABELLEN ]; -char modestr[8 ]; -char *cp; - +int i;if (!capable (CAP_MAC_OVERRIDE ))return -EPERM ;
@@ -208,7 +287,7 @@ static ssize_t smk_write_load(struct file *file, const char __user *buf,* 80 characters per line ought to be enough. */ if (count >SMACK_LIST_MAX *80 ) -return -ENOMEM ; +return -EFBIG ; data =kzalloc (count +1 ,GFP_KERNEL );if (data ==NULL )
@@ -221,30 +300,94 @@ static ssize_t smk_write_load(struct file *file, const char __user *buf, data[count] ='\0' ; -for (cp = data -1 ; cp !=NULL ; cp =strchr (cp +1 ,'\n' )) { -if (*++cp =='\0' ) +for (i =0 ; i < count && data[i]; i++) { +if (!isgraph (data[i]) && !isspace (data[i])) + goto out; + +if (isblank (data[i])) { + *state = blank; +/* A token-blank trasition */ +if (*prevstate != blank) + *prevtoken = *prevstate; + }else { +if (data[i] =='\n' ) + *state = newline; +/* A blank-token transition */ +else if (*prevstate == blank) + *state = *prevtoken +1 ; +else + *state = *prevstate; + } + +switch (*state) { +case bol: +case subject: +if (*label_len >=SMK_MAXLEN ) + goto out; + subjectstr[(*label_len)++] = data[i]; + *prevstate = subject;break ; -if (sscanf (cp,"%23s %23s %7s\n" , subjectstr, objectstr, - modestr) !=3 ) + +case object: +if (*prevstate == blank) { + subjectstr[*label_len] ='\0' ; + *label_len =0 ; + } + +if (*label_len >=SMK_MAXLEN ) + goto out; + objectstr[(*label_len)++] = data[i]; + *prevstate = object; +break ; + +case access: +if (*prevstate == blank) { + objectstr[*label_len] ='\0' ; + *label_len =0 ; + } + +if (data[i] =='R' || data[i] =='r' ) + rule->smk_access |=MAY_READ ; +else if (data[i] =='W' || data[i] =='w' ) + rule->smk_access |=MAY_WRITE ; +else if (data[i] =='X' || data[i] =='x' ) + rule->smk_access |=MAY_EXEC ; +else if (data[i] =='A' || data[i] =='a' ) + rule->smk_access |=MAY_APPEND ; +else if (data[i] =='-' ) +/* No-Op, '-' is a placeholder */ ; +else + goto out; + + *prevstate = *prevtoken = access;break ; - rule.smk_subject =smk_import (subjectstr,0 ); -if (rule.smk_subject ==NULL ) + +case newline: +if (*prevtoken != access || data[i] !='\n' ) + goto out; + + rule->smk_subject =smk_import (subjectstr,0 ); +if (rule->smk_subject ==NULL ) + goto out; + + rule->smk_object =smk_import (objectstr,0 ); +if (rule->smk_object ==NULL ) + goto out; + +smk_set_access (rule); + +/* Reset everything, a new rule will come */ +memset (load_state,0 ,sizeof (*load_state));break ; - rule.smk_object =smk_import (objectstr,0 ); -if (rule.smk_object ==NULL ) + +case blank: + *prevstate = blank;break ; - rule.smk_access =0 ; -if (strpbrk (modestr,"rR" ) !=NULL ) - rule.smk_access |=MAY_READ ; -if (strpbrk (modestr,"wW" ) !=NULL ) - rule.smk_access |=MAY_WRITE ; -if (strpbrk (modestr,"xX" ) !=NULL ) - rule.smk_access |=MAY_EXEC ; -if (strpbrk (modestr,"aA" ) !=NULL ) - rule.smk_access |=MAY_APPEND ; -smk_set_access (&rule); + } } + rc = count; +out:kfree (data);return rc; }
@@ -254,7 +397,7 @@ static const struct file_operations smk_load_ops = { .read = seq_read, .llseek = seq_lseek, .write = smk_write_load, - .release = seq_release + .release = smk_release_load, };/**
@@ -924,6 +1067,7 @@ static int __init init_smk_fs(void) } } +sema_init (&smack_write_sem,1 );smk_cipso_doi ();return err;
--
Ahmed S. Darwish
Homepage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser(2)
...
Quoted 2 lines
I've double-checked the code for any possible off-by-one/overflow errors.
...
Two things caught my eye.
...
security/smack/smackfs.c static ssize_t smk_write_load(struct file *file, const char __user *buf, · line 327
Quoted 5 lines
security/smack/smackfs.c +5
-0+case bol: +case subject: +if (*label_len >=SMK_MAXLEN ) + goto out; + subjectstr[(*label_len)++] = data[i];
Why is the '>' necessary? Could it happen that you had incremented past the point of equality?
If that could not happen, then in my oppinion '>=' is very misleading when '==' is really what is needed.
...
security/smack/smackfs.c static ssize_t smk_write_load(struct file *file, const char __user *buf, · line 335
Quoted 5 lines
security/smack/smackfs.c +5
-0+case object: +if (*prevstate == blank) { + subjectstr[*label_len] ='\0' ; + *label_len =0 ; + }
I wonder why it is valid to uncritically use the already incremented label_len here, without checking its value (like is done above).
It seems strangely asymmetrical. I'm not saying it's wrong, because there may be a subtle reason as to why it's not, but if that's the case then I think that subtle reason should be documented with a comment.
...
security/smack/smackfs.c static ssize_t smk_write_load(struct file *file, const char __user *buf, · line 347
Quoted 5 lines
security/smack/smackfs.c +5
-0+case access: +if (*prevstate == blank) { + objectstr[*label_len] ='\0' ; + *label_len =0 ; + }
Same applies here.
Signature · 1 line
/ jakob
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser(2)
Quoted 14 lines · Jakob Oestergaard
...
Quoted 2 lines
I've double-checked the code for any possible off-by-one/overflow errors.
...
Two things caught my eye.
...
security/smack/smackfs.c static ssize_t smk_write_load(struct file *file, const char __user *buf, · line 327
Quoted 5 lines
security/smack/smackfs.c +5
-0+case bol: +case subject: +if (*label_len >=SMK_MAXLEN ) + goto out; + subjectstr[(*label_len)++] = data[i];
Quoted 6 lines
Why is the '>' necessary? Could it happen that you had incremented past the point of equality?
If that could not happen, then in my oppinion '>=' is very misleading when '==' is really what is needed.
Indeed, you're absolutely right.
Quoted 7 lines
...
security/smack/smackfs.c static ssize_t smk_write_load(struct file *file, const char __user *buf, · line 335
Quoted 5 lines
security/smack/smackfs.c +5
-0+case object: +if (*prevstate == blank) { + subjectstr[*label_len] ='\0' ; + *label_len =0 ; + }
Quoted 7 lines
I wonder why it is valid to uncritically use the already incremented
label_lenhere, without checking its value (like is done above).It seems strangely asymmetrical. I'm not saying it's wrong, because there may be a subtle reason as to why it's not, but if that's the case then I think that subtle reason should be documented with a comment.
Maximum value label_len could reach is "SMK_MAXLEN". The subjectstr and objectstr arrays are of "SMK_MAXLEN + 1" length. So I think no danger is here.
Yes, this deserved a comment.
Quoted 11 lines
...
security/smack/smackfs.c static ssize_t smk_write_load(struct file *file, const char __user *buf, · line 347
Quoted 5 lines
security/smack/smackfs.c +5
-0+case access: +if (*prevstate == blank) { + objectstr[*label_len] ='\0' ; + *label_len =0 ; + }Same applies here.
/ jakob
Good spots, thanks a lot for the review.
Regards,
Signature · 3 lines
Ahmed S. Darwish Homepage: http://darwish.07.googlepages.com Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser(2)
In reply to Ahmed S. DarwishHi!
Quoted 3 lines
Quoted 3 lines
A Smack Rule in an "egrep" format is:
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
Perhaps you should make it space, not 'space or tab', and only allow lowercase permissions? That way, parser will be slightly simpler, and you'll still have a chance to use 'R' as 'slightly different r'.
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser(2)
Hi Pavel,
Quoted 10 lines · Pavel Machek
Hi!
Quoted 3 lines
Quoted 3 lines
A Smack Rule in an "egrep" format is:
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
Perhaps you should make it space, not 'space or tab', and only allow lowercase permissions? That way, parser will be slightly simpler, and you'll still have a chance to use 'R' as 'slightly different r'.
Thanks for your care about this. It seems not a lot of people have noticed, but to stop any objections not related to the core smack code, Casey decided to let the parsing be done in a user-space utility that sends the rules to the kernel in a predefined strict format.
You can find how the whole story in the smackv11 announcement here:
http://article.gmane.org/gmane.linux.kernel.lsm/4463
Regards,
Signature · 3 lines
Ahmed S. Darwish Homepage: http://darwish.07.googlepages.com Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Ahmed S. DarwishQuoted 11 lines · Ahmed S. Darwish
Quoted 4 lines · Casey Schaufler
Still to come:
- Final cleanup of
smack_load_writeandsmack_cipso_write.Hi All,
After agreeing with Casey on the "load" input grammar yesterday, here's the final grammar and its parser (which needs more testing):
Quoted 8 lines
A Smack Rule in an "egrep" format is:
"^[:space:]*Subject[:space:]+Object[:space:]+[rwxaRWXA-]+[:space:]*\n"
where Subject/Object strings are in the form:
"^[^/[:space:][:cntrl:]]{1,SMK_MAXLEN}$"
...
Can you limit this to 7bit ASCII and use isascii() somewhere?
Otherwise I'd expect funny things to happen when you e.g. use isspace() on the UTF-8 encoded character à.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 3 lines · Adrian Bunk
Can you limit this to 7bit ASCII and use
isascii()somewhere?Otherwise I'd expect funny things to happen when you e.g. use
isspace()on the UTF-8 encoded character à.
Actually, you don't need to. You tell them it expects UTF-8 encoded strings and be done with it. All US-ASCII characters from 0 through 127 (IE: high bit clear) are exactly the same in UTF-8, and UTF-8 special characters have the high bit set in all bytes. Therefore you just assume that anything with the high bit set is part of a word and you can handle basic UTF-8. (It doesn't work on special UTF-8 space characters like nonbreaking space and similar, but handling those is significantly more complicated).
Cheers,
Kyle Moffett
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 6 lines · Kyle Moffett
Quoted 4 lines · Adrian Bunk
Can you limit this to 7bit ASCII and use
isascii()somewhere?Otherwise I'd expect funny things to happen when you e.g. use
isspace()on the UTF-8 encoded character à.
Quoted 8 lines
Actually, you don't need to. You tell them it expects UTF-8 encoded strings and be done with it. All US-ASCII characters from 0 through 127 (IE: high bit clear) are exactly the same in UTF-8, and UTF-8 special characters have the high bit set in all bytes. Therefore you just assume that anything with the high bit set is part of a word and you can handle basic UTF-8. (It doesn't work on special UTF-8 space characters like nonbreaking space and similar, but handling those is significantly more complicated).
The documentations says:
"Smack labels cannot contain unprintable characters or the "/" (slash)
character."
What you propose might contain unprintable characters, and it might even be invalid UTF-8.
Quoted 2 lines
Cheers,
Kyle Moffett
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 6 lines
The documentations says:
"Smack labels cannot contain unprintable characters or the "/" (slash)
character."What you propose might contain unprintable characters, and it might even be invalid UTF-8.
Just like filenames then. I haven't noticed the vfs explode as a result
Alan
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Adrian BunkHi,
Quoted 16 lines · Adrian Bunk
Quoted 6 lines · Kyle Moffett
Quoted 4 lines · Adrian Bunk
Can you limit this to 7bit ASCII and use
isascii()somewhere?Otherwise I'd expect funny things to happen when you e.g. use
isspace()on the UTF-8 encoded character ??.Quoted 8 lines
Actually, you don't need to. You tell them it expects UTF-8 encoded strings and be done with it. All US-ASCII characters from 0 through 127 (IE: high bit clear) are exactly the same in UTF-8, and UTF-8 special characters have the high bit set in all bytes. Therefore you just assume that anything with the high bit set is part of a word and you can handle basic UTF-8. (It doesn't work on special UTF-8 space characters like nonbreaking space and similar, but handling those is significantly more complicated).
Quoted 7 lines
The documentations says:
"Smack labels cannot contain unprintable characters or the "/" (slash)
character."What you propose might contain unprintable characters, and it might even be invalid UTF-8.
As far as I understand the problem now, isspace() accepts the 0xa0 character which might collide with some of UTF-8 encoded characters cause the high bit is set.
I used "if (!isspace(c) && !isgraph(c)) return -EINVAL;" to test rules' characters validity which seems not enough. I'll add !isascii(c) in the condition and ask Casey to change the documentation to be something like:
Smack labels are represented in ASCII characters, they cannot contain unprintable characters or the '/' (slash) character.
and in write():
if (!isascii(c) return -EINVAL;
if (!isspace(c) && !isgraph(c)) return -EINVAL;
This satisfy above customized labels rule, right ?
Regards,
--
Ahmed S. Darwish
Homepage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 35 lines · Ahmed S. Darwish
Hi,
Quoted 16 lines · Adrian Bunk
Quoted 6 lines · Kyle Moffett
Quoted 4 lines · Adrian Bunk
Can you limit this to 7bit ASCII and use
isascii()somewhere?Otherwise I'd expect funny things to happen when you e.g. use
isspace()on the UTF-8 encoded character ??.Quoted 8 lines
Actually, you don't need to. You tell them it expects UTF-8 encoded strings and be done with it. All US-ASCII characters from 0 through 127 (IE: high bit clear) are exactly the same in UTF-8, and UTF-8 special characters have the high bit set in all bytes. Therefore you just assume that anything with the high bit set is part of a word and you can handle basic UTF-8. (It doesn't work on special UTF-8 space characters like nonbreaking space and similar, but handling those is significantly more complicated).
Quoted 6 lines
The documentations says:
"Smack labels cannot contain unprintable characters or the "/" (slash)
character."What you propose might contain unprintable characters, and it might even be invalid UTF-8.
As far as I understand the problem now,
isspace()accepts the0xa0character which might collide with some of UTF-8 encoded characters cause the high bit is set.I used "if (!isspace(c) && !isgraph(c)) return
-EINVAL;" to test rules' characters validity which seems not enough. I'll add !isascii(c) in the condition and ask Casey to change the documentation to be something like:
Quoted 8 lines
Smack labels are represented in ASCII characters, they cannot contain unprintable characters or the '/' (slash) character.
and in
write():
if (!isascii(c) return-EINVAL;
if (!isspace(c) && !isgraph(c)) return-EINVAL;This satisfy above customized labels rule, right ?
It should work for all charsets you'll usually have to handle.
It would not work if someone would e.g. give you UTF-16 encoded strings, but I don't see this happening in practice.
Quoted 1 line
Regards,
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 39 lines · Adrian Bunk
Quoted 38 lines · Ahmed S. Darwish
Hi,
Quoted 16 lines · Adrian Bunk
Quoted 6 lines · Kyle Moffett
Quoted 4 lines · Adrian Bunk
Can you limit this to 7bit ASCII and use
isascii()somewhere?Otherwise I'd expect funny things to happen when you e.g. use
isspace()on the UTF-8 encoded character ??.Quoted 8 lines
Actually, you don't need to. You tell them it expects UTF-8 encoded strings and be done with it. All US-ASCII characters from 0 through 127 (IE: high bit clear) are exactly the same in UTF-8, and UTF-8 special characters have the high bit set in all bytes. Therefore you just assume that anything with the high bit set is part of a word and you can handle basic UTF-8. (It doesn't work on special UTF-8 space characters like nonbreaking space and similar, but handling those is significantly more complicated).
Quoted 6 lines
The documentations says:
"Smack labels cannot contain unprintable characters or the "/" (slash)
character."What you propose might contain unprintable characters, and it might even be invalid UTF-8.
As far as I understand the problem now,
isspace()accepts the0xa0character which might collide with some of UTF-8 encoded characters cause the high bit is set.I used "if (!isspace(c) && !isgraph(c)) return
-EINVAL;" to test rules' characters validity which seems not enough. I'll add !isascii(c) in the condition and ask Casey to change the documentation to be something like:Smack labels are represented in ASCII characters, they cannot contain unprintable characters or the '/' (slash) character.
Quoted 8 lines
Quoted 5 lines
and in
write():
if (!isascii(c) return-EINVAL;
if (!isspace(c) && !isgraph(c)) return-EINVAL;This satisfy above customized labels rule, right ?
It should work for all charsets you'll usually have to handle.
I admit I'm not experienced in such encoding stuff, but shouldn't the ASCII and the ASCII-compatible UTF-8 encodings be enough for the labels ?
Quoted 2 lines
It would not work if someone would e.g. give you UTF-16 encoded strings, but I don't see this happening in practice.
Won't this complicate the code too much ?
Signature · 3 lines
Ahmed S. Darwish Homepage: http://darwish.07.googlepages.com Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 9 lines · Ahmed S. Darwish
Quoted 2 lines · Adrian Bunk
Quoted 1 line · Ahmed S. Darwish
As far as I understand the problem now,
isspace()accepts the0xa0character which might collide with some of UTF-8 encoded characters cause the high bit is set.I admit I'm not experienced in such encoding stuff, but shouldn't the ASCII and the ASCII-compatible UTF-8 encodings be enough for the labels?
Quoted 1 line
It would not work if someone would e.g. give you UTF-16 encoded strings, but I don't see this happening in practice.
Won't this complicate the code too much ?
Well the VFS (for example) certainly doesn't support any encodings other than various extended-ASCII forms (which includes UTF-8). Something like UTF-16 has extra null characters in-between every normal character, and as such would fail completely if passed to the VFS.
Personally I think that isspace() accepting character 0xA0 is a bug, as there are several variants of extended ASCII only one of which has that character as a space. Others have it as á (accented A), etc. In addition the "canonical" internal text format of the kernel is UTF-8 as that encoding can represent any character in any other encoding and it is backwards-compatible with traditional ASCII.
Cheers,
Kyle Moffett
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 13 lines · Kyle Moffett
Quoted 12 lines · Ahmed S. Darwish
Quoted 4 lines · Adrian Bunk
Quoted 3 lines · Ahmed S. Darwish
As far as I understand the problem now,
isspace()accepts the0xa0character which might collide with some of UTF-8 encoded characters cause the high bit is set.I admit I'm not experienced in such encoding stuff, but shouldn't the ASCII and the ASCII-compatible UTF-8 encodings be enough for the labels?
Quoted 2 lines
It would not work if someone would e.g. give you UTF-16 encoded strings, but I don't see this happening in practice.
Quoted 6 lines
Quoted 1 line
Won't this complicate the code too much ?
Well the VFS (for example) certainly doesn't support any encodings other than various extended-ASCII forms (which includes UTF-8). Something like UTF-16 has extra null characters in-between every normal character, and as such would fail completely if passed to the VFS.
Good point.
Quoted 3 lines
Personally I think that
isspace()accepting character0xA0is a bug, as there are several variants of extended ASCII only one of which has that character as a space. Others have it as á (accented A), etc.
But even then Smack would still have a similar problem with isgraph().
Quoted 7 lines
In addition
the "canonical" internal text format of the kernel is UTF-8 as that encoding can represent any character in any other encoding and it is backwards-compatible with traditional ASCII.Cheers,
Kyle Moffett-
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 21 lines · Adrian Bunk
Quoted 13 lines · Kyle Moffett
Quoted 12 lines · Ahmed S. Darwish
Quoted 4 lines · Adrian Bunk
Quoted 3 lines · Ahmed S. Darwish
As far as I understand the problem now,
isspace()accepts the0xa0character which might collide with some of UTF-8 encoded characters cause the high bit is set.I admit I'm not experienced in such encoding stuff, but shouldn't the ASCII and the ASCII-compatible UTF-8 encodings be enough for the labels?
Quoted 2 lines
It would not work if someone would e.g. give you UTF-16 encoded strings, but I don't see this happening in practice.
Quoted 6 lines
Quoted 1 line
Won't this complicate the code too much ?
Well the VFS (for example) certainly doesn't support any encodings other than various extended-ASCII forms (which includes UTF-8). Something like UTF-16 has extra null characters in-between every normal character, and as such would fail completely if passed to the VFS.
Quoted 8 lines
Good point.
Quoted 3 lines
Personally I think that
isspace()accepting character0xA0is a bug, as there are several variants of extended ASCII only one of which has that character as a space. Others have it as ?? (accented A), etc.But even then Smack would still have a similar problem with
isgraph().
Great, To summarize the discussion. Will there be a problem in accepting ASCII and the UTF-8 ASCII _subset_ _only_ and return -EINVAL for all other cases/ecnodings ?.
i.e. The fragment I sent in a previous message:
/* Filter UTF-8 non-ascii compatible bytes (> 0x7F) */ if (!isascii(c)) return -EINVAL;
/* Filter unwanted ascii chars */
if (!isspace(c) && !isgraph(c)) return -EINVAL;
Quoted 11 lines
Quoted 7 lines
In addition
the "canonical" internal text format of the kernel is UTF-8 as that encoding can represent any character in any other encoding and it is backwards-compatible with traditional ASCII.Cheers,
Kyle Moffett-cu
Adrian
--
Ahmed S. Darwish
Homepage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 28 lines · Ahmed S. Darwish
Quoted 27 lines · Adrian Bunk
Quoted 13 lines · Kyle Moffett
Quoted 12 lines · Ahmed S. Darwish
Quoted 4 lines · Adrian Bunk
Quoted 3 lines · Ahmed S. Darwish
As far as I understand the problem now,
isspace()accepts the0xa0character which might collide with some of UTF-8 encoded characters cause the high bit is set.I admit I'm not experienced in such encoding stuff, but shouldn't the ASCII and the ASCII-compatible UTF-8 encodings be enough for the labels?
Quoted 2 lines
It would not work if someone would e.g. give you UTF-16 encoded strings, but I don't see this happening in practice.
Quoted 6 lines
Quoted 1 line
Won't this complicate the code too much ?
Well the VFS (for example) certainly doesn't support any encodings other than various extended-ASCII forms (which includes UTF-8). Something like UTF-16 has extra null characters in-between every normal character, and as such would fail completely if passed to the VFS.
Good point.
Quoted 3 lines
Personally I think that
isspace()accepting character0xA0is a bug, as there are several variants of extended ASCII only one of which has that character as a space. Others have it as ?? (accented A), etc.
Quoted 6 lines
Quoted 2 lines
But even then Smack would still have a similar problem with
isgraph().Great, To summarize the discussion. Will there be a problem in accepting ASCII and the UTF-8 ASCII _subset_ _only_ and return
-EINVALfor all other cases/ecnodings ?.
The UTF-8 ASCII subset is the complete ASCII.
Quoted 7 lines
i.e. The fragment I sent in a previous message:
/* Filter UTF-8 non-ascii compatible bytes (>
0x7F) */ if (!isascii(c)) return-EINVAL;
/* Filter unwanted ascii chars */
if (!isspace(c) && !isgraph(c)) return-EINVAL;
...
As I already said, this should work fine.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedRe: [PATCH] Smackv10: Smack rules grammar + their stateful parser
Quoted 10 lines · Adrian Bunk
Quoted 4 lines · Ahmed S. Darwish
Great, To summarize the discussion. Will there be a problem in accepting ASCII and the UTF-8 ASCII _subset_ _only_ and return
-EINVALfor all other cases/ecnodings ?.The UTF-8 ASCII subset is the complete ASCII.
Quoted 2 lines
i.e. The fragment I sent in a previous message:
Quoted 8 lines
Quoted 5 lines
/* Filter UTF-8 non-ascii compatible bytes (>
0x7F) */ if (!isascii(c)) return-EINVAL;
/* Filter unwanted ascii chars */
if (!isspace(c) && !isgraph(c)) return-EINVAL;
...As I already said, this should work fine.
I thought you were objecting not handling the remaining non-ASCII UTF-8 bytes. Everything is clear now. A Big thank you to everybody in this thread for your effort on this :).
Regards,
Signature · 3 lines
Ahmed S. Darwish Homepage: http://darwish.07.googlepages.com Blog: http://darwish-07.blogspot.com
Re: [PATCH] Smackv10: Smack rules grammar + their stateful parser
In reply to Kyle MoffettQuoted 2 lines · Kyle Moffett
Personally I think that
isspace()accepting character0xA0is a bug
I think I agree with you. As far as the kernel is concerned, "isspace()" should just accept the obvious spaces (hardspace, tab, newline), and *perhaps* the VT/FF kind of things.
You should realize that the kernel <ctype.h> thing is *ancient*. It's basically there from v0.01, and while the really original one (I just checked) had all the non-ascii characters not trigger anything, it was converted to be latin1 in the 2.1.x timeframe.
That's a *loong* time ago. Way before UTF-8 and other things were really common.
So we should probably just make all the upper 128 bytes go back to "don't trigger anything in ctype.h" - they'd not be spaces, but they'd not be control characters or anything else either.
Linus
PATCH 1/1 Fix isspace() and other ctype.h functions to ignore chars 128-255
Originally isspace() and other similar functions in ctype.h ignored any character with the high bit set; however this was changed during the linux 2.1 days to map Latin-1. As following Latin-1 will most likely break UTF-8 any any *other* encoding that is backwards-compatible with 7-bit-ASCII, change ctype.c to ignore such characters completely (the way they were before). Linus seems to think this is a good thing, and he's the one that wrote the code in the first place.
---
Quoted 10 lines · Linus Torvalds
Quoted 1 line · Kyle Moffett
Personally I think that
isspace()accepting character0xA0is a bugI think I agree with you. As far as the kernel is concerned, "
isspace()" should just accept the obvious spaces (hardspace, tab, newline), and *perhaps* the VT/FF kind of things.You should realize that the kernel <
ctype.h> thing is *ancient*. It's basically there from v0.01, and while the really original one (I just checked) had all the non-ascii characters not trigger anything, it was converted to be latin1 in the 2.1.x timeframe.That's a *loong* time ago. Way before UTF-8 and other things were really common.
So we should probably just make all the upper 128 bytes go back to "don't trigger anything in
ctype.h" - they'd not be spaces, but they'd not be control characters or anything else either.
lib/ctype.c | 17 +++++++++++------ 1 files changed, 11 insertions(+), 6 deletions(-)
lib/ctype.c +11 -6
@@ -24,13 +24,18 @@ _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */ _L,_L,_L,_L,_L,_L,_L,_L,/* 104-111 */ _L,_L,_L,_L,_L,_L,_L,_L,/* 112-119 */ _L,_L,_L,_P,_P,_P,_P,_C,/* 120-127 */ + +/* +* None of these match any type bits to avoid screwing up UTF-8 or any other +* 7-bit-ASCII-compatible encoding. +*/ 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 128-143 */ 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 144-159 */ -_S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,/* 160-175 */ -_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,/* 176-191 */ -_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,/* 192-207 */ -_U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L,/* 208-223 */ -_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,/* 224-239 */ -_L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */ +0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 160-175 */ +0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 176-191 */ +0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 192-207 */ +0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 208-223 */ +0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 224-239 */ +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* 240-255 */EXPORT_SYMBOL (_ctype);