Skip to contentSkip to conversation

prctl.2: Add description of Intel MPX calls

PATCH Michael Kerrisk (man-pages) 2 participants 2 messages Jan 8, 2015 1 message not in the archive Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

[CC += Qiaowei Ren, in case they might comment]

Hello Dave,

Thanks for the patch. Quite a number of queries below. Could you address those and resubmit?

Quoted 4 lines · Dave Hansen

From: Dave Hansen <[email protected]>

(apologies if this is a duplicate mail, I didn't see my first one
hit either of the lists, so I assume Intel's mail relays ate it)

(Actually, both mails did arrive.)

Quoted 7 lines

The 3.19 kernel will have support for Intel MPX, including a pair of new prctl() calls for enabling and disabling the kernel's management of the "bounds tables". Add some descriptions of the interface.

The kernel patches were written by myself and another Intel developer.

(Thanks -- getting that piece of info is always helpful when I consider how carefully I need to review a patch.)

man2/prctl.2 value.

Quoted 20 lines
---
 man2/prctl.2 | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
man2/prctl.2 +1 -0
index 4efabcf..6555d7a 100644
@@ -47,6 +47,7 @@
 .\"                             PR_GET_TIMERSLACK
 .\" 2013-01-10 Kees Cook, document PR_SET_PTRACER
 .\" 2012-02-04 Michael kerrisk, document PR_{SET,GET}_CHILD_SUBREAPER
+.\" 2014-11-10 Dave Hansen, Document PR_MPX_{EN,DIS}ABLE_MANAGEMENT
 .\"
 .\"
 .TH PRCTL 2 2014-04-14 "Linux" "Linux Programmer's Manual"
@@ -737,6 +738,21 @@ value.

It looks like this patch is misplaced in the source. Probably best to place the new text just above the RETURN VALUE section.

man2/prctl.2 value. · line 742

Quoted 5 lines
man2/prctl.2 +2 -0
 The requirements for the address are the same as for the
 .BR PR_SET_MM_START_BRK
 option.
+.TP
+.BR PR_MPX_ENABLE_MANAGEMENT / PR_MPX_DISABLE_MANAGEMENT

Make this:

.BR PR_MPX_ENABLE_MANAGEMENT / PR_MPX_DISABLE_MANAGEMENT " (since Linux 3.19)"

And then just after please add the following source comments (for handy reference on future page edits):

.\" commit fe3d197f84319d3bce379a9c0dc17b1f48ad358c
.\" See also http://lwn.net/Articles/582712/
.\" See also https://gcc.gnu.org/wiki/Intel%20MPX%20support%20in%20the%20GCC%20compiler

man2/prctl.2 value. · line 744

Quoted 2 lines
man2/prctl.2 +2 -0
+
+Control the kernel's management of Intel Memory Protection eXtensions

s/Memory Protection eXtensions/Memory Protection eXtensions (MPX)/

man2/prctl.2 value. · line 746

Quoted 2 lines
man2/prctl.2 +2 -0
+tables.  When management is enabled, the kernel will take over allocation
+and freeing of MPX bounds tables.

In between or after the two preceding sentences, I think it would be good to some text explaining why MPX is useful. Something along the lines of this text from Jon Corbet's article at http://lwn.net/Articles/582712/,
though you may want to cut down, update, or improve the text.

[[
MPX is a hardware-assisted mechanism for performing bounds checking on pointer accesses. The hardware, following direction from software, maintains a table of pointers in use and the range of accessible memory (the "bounds") associated with each. Whenever a pointer is dereferenced, special instructions can be used to ensure that the program is accessing memory within the range specified for that particular pointer.

When a bounds violation is detected, the processor will trap into the kernel. The kernel, in turn, will turn the trap into a SIGSEGV signal to be delivered to the application, similar to other types of memory access errors. Applications that look at the siginfo structure passed to the signal handler from the kernel will be able to recognize a bounds error by checking the si_code field for the new SEGV_BNDERR value. The offending address will be stored in si_addr, while the bounds in effect at the time of the trap will be stored in si_lower and si_upper.
]]

man2/prctl.2 value. · line 749

Quoted 3 lines
man2/prctl.2 +3 -0
+
+The application must have allocated virtual space for the bounds
+directory and placed its location in bndcfgu before enabling management.

s/bndcfgu/the bndcfgu register/

So, let me make sure I understood the previous sentence correctly. Is this a good formulation:

Before enabling MPX management using PR_MPX_ENABLE_MANAGEMENT, the application must first have allocated a user-space buffer for the bounds directory and placed the location of that directory in the bndcfgu register.
?

If so, I think the patch could be reworded closer to that text, since it makes things a little clearer.

By the way, what size should allocated for the buffer? And what can wrong if the allocated buffer is too small?

man2/prctl.2 value. · line 750

Quoted 1 line
man2/prctl.2 +1 -0
+Once management is enabled, the bounds directory may not be moved.

A few things are not so clear to me, even after reading Jon's article and Documentation/x86/intel_mpx.txt. In particular, who has responsibility for populating the bounds directory, and what is its format? Is this all invisible to the application? (Because I'm not quite clear on these points, I'm not sure if anything should be said in the man page.)

man2/prctl.2 value. · line 752

Quoted 2 lines
man2/prctl.2 +2 -0
+
+These calls will fail if MPX support is not present in the CPU or the

I suggest:
s/if MPX support is not present in the CPU/if the CPU does not support MPX/

man2/prctl.2 value. · line 753

Quoted 1 line
man2/prctl.2 +1 -0
+kernel does not had support for MPX enabled.

s/had/have/

And add the following:

Kernel support for MPX is enabled via the
.BR CONFIG_X86_INTEL_MPX
configuration option.
You can check whether the CPU supports MPX using the following command:

cat /proc/cpuinfo | grep ' mpx '

man2/prctl.2 value. · line 755

Quoted 2 lines
man2/prctl.2 +2 -0
+
+All threads in a process are affected by these calls.

Please add here:

For further information on Intel MPX, see the kernel source file .IR Documentation/x86/intel_mpx.txt .

man2/prctl.2 value. · line 758

Quoted 3 lines
man2/prctl.2 +0 -0
 .P
 The following options are available since Linux 3.5.
 .\" commit fe8c7f5cbf91124987106faa3bdf0c8b955c4cf7

Could you add some text to the patch to explain the ENXIO error?

What are the semantics of the MPX state with respect to the child of a fork(2)? (Probably, the man page needs to detail this.)

What happens to the MPX state during an execve(2)? (Probably, the man page needs to detail this.)

Is there a way to discover the current state (enabled/disabled) of MPX management? (It appears not, but I would like to confirm.)

I see that the prctl() operations fail to check that unused arguments are zero. See my patch sent in a separate mail to LKML et al.

Cheers,

Michael

Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 2 lines · Michael Kerrisk (man-pages)

Thanks for the patch. Quite a number of queries below. Could you address those and resubmit?

Absolutely, thanks for the detailed review!

I'll address most of your comments in the new patch.

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+The application must have allocated virtual space for the bounds
+directory and placed its location in bndcfgu before enabling management.

s/bndcfgu/the bndcfgu register/

So, let me make sure I understood the previous sentence correctly. Is this a good formulation:

Quoted 8 lines

Before enabling MPX management using PR_MPX_ENABLE_MANAGEMENT, the application must first have allocated a user-space buffer for the bounds directory and placed the location of that directory in the bndcfgu register.
?

If so, I think the patch could be reworded closer to that text, since it makes things a little clearer.

That looks good to me.

Quoted 2 lines

By the way, what size should allocated for the buffer? And what can wrong if the allocated buffer is too small?

4MB on 32-bit and 2GB on 64-bit.

If the buffer is too small, the hardware might attempt to read a non-allocated memory area (and SEGV). The kernel would notice that the buffer is too small probably during the first bounds fault touching the unallocated area of the table.

Quoted 7 lines
Quoted 1 line

+Once management is enabled, the bounds directory may not be moved.

A few things are not so clear to me, even after reading Jon's article and Documentation/x86/intel_mpx.txt. In particular, who has responsibility for populating the bounds directory, and what is its format? Is this all invisible to the application? (Because I'm not quite clear on these points, I'm not sure if anything should be said in the man page.)

This is all best spelled out in the documentation available here:

https://software.intel.com/en-us/intel-isa-extensions

The compiler generates code which loads the bounds values in to the bounds registers. There are special instructions that "spill" the bounds registers in to the tables.

So, the instructions in the application do it, but it's transparent and doesn't require any source modifications.

Quoted 5 lines
Quoted 3 lines

.P
The following options are available since Linux 3.5. .\" commit fe8c7f5cbf91124987106faa3bdf0c8b955c4cf7

Could you add some text to the patch to explain the ENXIO error?

Sure.

Quoted 2 lines

What are the semantics of the MPX state with respect to the child of a fork(2)? (Probably, the man page needs to detail this.)

The tables are inherited just as any other memory contents are. The register contents are inherited just as any other register would be.

Quoted 2 lines

What happens to the MPX state during an execve(2)? (Probably, the man page needs to detail this.)

The tables go away at exec just like any other memory. The BNDCFGU register (the one holding the location of the tables) is zeroed.

Quoted 2 lines

Is there a way to discover the current state (enabled/disabled) of MPX management? (It appears not, but I would like to confirm.)

There is not. It's not something I really expect an app to want to turn on and off or need to query. It might not be a bad idea to expose it in /proc somehow.

Also, FWIW, you could always attach with a debugger and check out the BNDCFGU contents if you _practically_ wanted to check for MPX being used. It wouldn't tell you if bounds management is enabled, but it would be a really good indicator.

Quoted 2 lines

I see that the prctl() operations fail to check that unused arguments are zero. See my patch sent in a separate mail to LKML et al.

OK, fair enough.