Skip to content

Commit c407c03

Browse files
Yoav ZachLinus Torvalds
authored andcommitted
[PATCH] binfmt_misc: improve calculation of interpreter's credentials
This patch allows for misc binaries to run with credentials and security token that are calculated according to the binaries, and not according to the interpreter, which is the legacy behavior of binfmt_misc. The way it is done is by calling prepare_binprm, which is where these attributes are calculated, before switching the 'file' field in the bprm from the binary to the interpreter. This feature should be used with care, since the interpreter will have root permissions when running a setuid binary owned by root. Please note - - Only root can register an interpreter with binfmt_misc. The feature is documented and the administrator is advised to handle it with care - The new feature is enabled only with a special flag in the registration string. When this flag is not specified the current behavior of binfmt_misc is kept - This is the only 'right' way for an interpreter to know the correct AT_SECURE value for the interpreted binary From: Chris Wright <chrisw@osdl.org> This patchset looks OK, except for one problem. It installs the fd (which could've been unreadable) without unsharing the ->files. So someone can use this to read unreadable yet executable files. Here's a patch which fixes that up. I added one bit that's commented out because I'm not positive if a final steal_locks() is needed. I did a fair amount of rearranging to simplify the error conditions relative to the fd_install(), and unshare_files(). From: Chris Wright <chrisw@osdl.org> I found that the intel patchset (and mine as well) leaked i_writecount on the original executed file. In addition, I verified that the steal_locks() bit is indeed needed. Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 79baf43 commit c407c03

2 files changed

Lines changed: 78 additions & 51 deletions

File tree

‎Documentation/binfmt_misc.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ Here is what the fields mean:
4848
the interpreter to execute non-readable binaries. This feature should
4949
be used with care - the interpreter has to be trusted not to emit
5050
the contents of the non-readable binary.
51+
'C' - credentials. Currently, the behavior of binfmt_misc is to calculate
52+
the credentials and security token of the new process according to
53+
the interpreter. When this flag is included, these attributes are
54+
calculated according to the binary. It also implies the 'O' flag.
55+
This feature should be used with care as the interpreter
56+
will run with root permissions when a setuid binary owned by root
57+
is run with binfmt_misc.
5158

5259

5360
There are some restrictions:

‎fs/binfmt_misc.c‎

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static int enabled = 1;
4040
enum {Enabled, Magic};
4141
#define MISC_FMT_PRESERVE_ARGV0 (1<<31)
4242
#define MISC_FMT_OPEN_BINARY (1<<30)
43+
#define MISC_FMT_CREDENTIALS (1<<29)
4344

4445
typedef struct {
4546
struct list_head list;
@@ -104,14 +105,12 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
104105
{
105106
Node *fmt;
106107
struct file * interp_file = NULL;
107-
struct file * binary_file = NULL;
108108
char iname[BINPRM_BUF_SIZE];
109109
char *iname_addr = iname;
110110
int retval;
111111
int fd_binary = -1;
112-
char fd_str[32];
113-
char * fdsp = fd_str;
114-
int is_open_bin;
112+
char fd_str[12];
113+
struct files_struct *files = NULL;
115114

116115
retval = -ENOEXEC;
117116
if (!enabled)
@@ -126,39 +125,56 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
126125
if (!fmt)
127126
goto _ret;
128127

129-
is_open_bin = (fmt->flags & MISC_FMT_OPEN_BINARY) ? 1 : 0;
128+
if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
129+
remove_arg_zero(bprm);
130+
}
130131

131-
if (is_open_bin) {
132+
if (fmt->flags & MISC_FMT_OPEN_BINARY) {
133+
char *fdsp = fd_str;
134+
135+
files = current->files;
136+
retval = unshare_files();
137+
if (retval < 0)
138+
goto _ret;
139+
if (files == current->files) {
140+
put_files_struct(files);
141+
files = NULL;
142+
}
132143
/* if the binary should be opened on behalf of the
133144
* interpreter than keep it open and assign descriptor
134145
* to it */
135-
fd_binary = get_unused_fd ();
146+
fd_binary = get_unused_fd();
136147
if (fd_binary < 0) {
137148
retval = fd_binary;
138-
goto _ret;
149+
goto _unshare;
139150
}
140-
snprintf (fd_str, sizeof(fd_str) - 1, "%d", fd_binary);
141-
} else {
142-
allow_write_access (bprm->file);
143-
fput (bprm->file);
144-
bprm->file = NULL;
145-
}
151+
fd_install(fd_binary, bprm->file);
146152

147-
/* Build args for interpreter */
148-
if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
149-
remove_arg_zero(bprm);
150-
}
153+
/* if the binary is not readable than enforce mm->dumpable=0
154+
regardless of the interpreter's permissions */
155+
if (permission(bprm->file->f_dentry->d_inode, MAY_READ, NULL))
156+
bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
157+
158+
allow_write_access(bprm->file);
159+
bprm->file = NULL;
151160

152-
if (is_open_bin) {
153161
/* make argv[1] be the file descriptor of the binary */
154-
retval = copy_strings_kernel (1, &fdsp, bprm);
162+
snprintf(fd_str, sizeof(fd_str), "%d", fd_binary);
163+
retval = copy_strings_kernel(1, &fdsp, bprm);
164+
if (retval < 0)
165+
goto _error;
166+
bprm->argc++;
167+
155168
} else {
169+
allow_write_access(bprm->file);
170+
fput(bprm->file);
171+
bprm->file = NULL;
156172
/* make argv[1] be the path to the binary */
157173
retval = copy_strings_kernel (1, &bprm->interp, bprm);
174+
if (retval < 0)
175+
goto _error;
176+
bprm->argc++;
158177
}
159-
if (retval < 0)
160-
goto _error;
161-
bprm->argc ++;
162178
retval = copy_strings_kernel (1, &iname_addr, bprm);
163179
if (retval < 0)
164180
goto _error;
@@ -170,47 +186,41 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
170186
if (IS_ERR (interp_file))
171187
goto _error;
172188

173-
174-
binary_file = bprm->file;
175189
bprm->file = interp_file;
176-
retval = prepare_binprm(bprm);
190+
if (fmt->flags & MISC_FMT_CREDENTIALS) {
191+
/*
192+
* No need to call prepare_binprm(), it's already been
193+
* done. bprm->buf is stale, update from interp_file.
194+
*/
195+
memset(bprm->buf, 0, BINPRM_BUF_SIZE);
196+
retval = kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
197+
} else
198+
retval = prepare_binprm (bprm);
177199

178200
if (retval < 0)
179201
goto _error;
180202

181-
if (is_open_bin) {
182-
/* if the binary is not readable than enforce mm->dumpable=0
183-
regardless of the interpreter's permissions */
184-
if (permission (binary_file->f_dentry->d_inode, MAY_READ, NULL)) {
185-
bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
186-
}
187-
/* install the binary's fd. it is done at the latest possible point
188-
* because once it is installed it will need to be sys_close()ed
189-
* in case of error.
190-
*/
191-
fd_install (fd_binary, binary_file);
192-
}
193-
194203
retval = search_binary_handler (bprm, regs);
195-
196204
if (retval < 0)
197-
goto _error_close_file;
205+
goto _error;
198206

207+
if (files) {
208+
steal_locks(files);
209+
put_files_struct(files);
210+
files = NULL;
211+
}
199212
_ret:
200213
return retval;
201-
202-
_error_close_file:
203-
if (fd_binary > 0) {
204-
sys_close (fd_binary);
205-
fd_binary = -1;
206-
bprm->file = NULL;
207-
}
208214
_error:
209215
if (fd_binary > 0)
210-
put_unused_fd (fd_binary);
216+
sys_close(fd_binary);
211217
bprm->interp_flags = 0;
218+
_unshare:
219+
if (files) {
220+
put_files_struct(current->files);
221+
current->files = files;
222+
}
212223
goto _ret;
213-
214224
}
215225

216226
/* Command parsers */
@@ -271,6 +281,13 @@ static inline char * check_special_flags (char * sfs, Node * e)
271281
p++;
272282
e->flags |= MISC_FMT_OPEN_BINARY;
273283
break;
284+
case 'C':
285+
p++;
286+
/* this flags also implies the
287+
open-binary flag */
288+
e->flags |= (MISC_FMT_CREDENTIALS |
289+
MISC_FMT_OPEN_BINARY);
290+
break;
274291
default:
275292
cont = 0;
276293
}
@@ -280,7 +297,7 @@ static inline char * check_special_flags (char * sfs, Node * e)
280297
}
281298
/*
282299
* This registers a new binary format, it recognises the syntax
283-
* ':name:type:offset:magic:mask:interpreter:'
300+
* ':name:type:offset:magic:mask:interpreter:flags'
284301
* where the ':' is the IFS, that can be chosen with the first char
285302
*/
286303
static Node *create_entry(const char __user *buffer, size_t count)
@@ -453,6 +470,9 @@ static void entry_status(Node *e, char *page)
453470
if (e->flags & MISC_FMT_OPEN_BINARY) {
454471
*dp ++ = 'O';
455472
}
473+
if (e->flags & MISC_FMT_CREDENTIALS) {
474+
*dp ++ = 'C';
475+
}
456476
*dp ++ = '\n';
457477

458478

0 commit comments

Comments
 (0)