Skip to content

Commit bb96a6f

Browse files
hallyntorvalds
authored andcommitted
userns: allow sethostname in a container
Changelog: Feb 23: let clone_uts_ns() handle setting uts->user_ns To do so we need to pass in the task_struct who'll get the utsname, so we can get its user_ns. Feb 23: As per Oleg's coment, just pass in tsk, instead of two of its members. Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com> Acked-by: "Eric W. Biederman" <ebiederm@xmission.com> Acked-by: Daniel Lezcano <daniel.lezcano@free.fr> Acked-by: David Howells <dhowells@redhat.com> Cc: James Morris <jmorris@namei.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3486740 commit bb96a6f

4 files changed

Lines changed: 12 additions & 15 deletions

File tree

‎include/linux/utsname.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static inline void get_uts_ns(struct uts_namespace *ns)
5454
}
5555

5656
extern struct uts_namespace *copy_utsname(unsigned long flags,
57-
struct uts_namespace *ns);
57+
struct task_struct *tsk);
5858
extern void free_uts_ns(struct kref *kref);
5959

6060
static inline void put_uts_ns(struct uts_namespace *ns)
@@ -71,12 +71,12 @@ static inline void put_uts_ns(struct uts_namespace *ns)
7171
}
7272

7373
static inline struct uts_namespace *copy_utsname(unsigned long flags,
74-
struct uts_namespace *ns)
74+
struct task_struct *tsk)
7575
{
7676
if (flags & CLONE_NEWUTS)
7777
return ERR_PTR(-EINVAL);
7878

79-
return ns;
79+
return tsk->nsproxy->uts_ns;
8080
}
8181
#endif
8282

‎kernel/nsproxy.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,11 @@ static struct nsproxy *create_new_namespaces(unsigned long flags,
6969
goto out_ns;
7070
}
7171

72-
new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
72+
new_nsp->uts_ns = copy_utsname(flags, tsk);
7373
if (IS_ERR(new_nsp->uts_ns)) {
7474
err = PTR_ERR(new_nsp->uts_ns);
7575
goto out_uts;
7676
}
77-
if (new_nsp->uts_ns != tsk->nsproxy->uts_ns) {
78-
put_user_ns(new_nsp->uts_ns->user_ns);
79-
new_nsp->uts_ns->user_ns = task_cred_xxx(tsk, user)->user_ns;
80-
get_user_ns(new_nsp->uts_ns->user_ns);
81-
}
8277

8378
new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
8479
if (IS_ERR(new_nsp->ipc_ns)) {

‎kernel/sys.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
11811181
int errno;
11821182
char tmp[__NEW_UTS_LEN];
11831183

1184-
if (!capable(CAP_SYS_ADMIN))
1184+
if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
11851185
return -EPERM;
11861186
if (len < 0 || len > __NEW_UTS_LEN)
11871187
return -EINVAL;

‎kernel/utsname.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ static struct uts_namespace *create_uts_ns(void)
3131
* @old_ns: namespace to clone
3232
* Return NULL on error (failure to kmalloc), new ns otherwise
3333
*/
34-
static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
34+
static struct uts_namespace *clone_uts_ns(struct task_struct *tsk,
35+
struct uts_namespace *old_ns)
3536
{
3637
struct uts_namespace *ns;
3738

@@ -41,8 +42,7 @@ static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
4142

4243
down_read(&uts_sem);
4344
memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
44-
ns->user_ns = old_ns->user_ns;
45-
get_user_ns(ns->user_ns);
45+
ns->user_ns = get_user_ns(task_cred_xxx(tsk, user)->user_ns);
4646
up_read(&uts_sem);
4747
return ns;
4848
}
@@ -53,8 +53,10 @@ static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
5353
* utsname of this process won't be seen by parent, and vice
5454
* versa.
5555
*/
56-
struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns)
56+
struct uts_namespace *copy_utsname(unsigned long flags,
57+
struct task_struct *tsk)
5758
{
59+
struct uts_namespace *old_ns = tsk->nsproxy->uts_ns;
5860
struct uts_namespace *new_ns;
5961

6062
BUG_ON(!old_ns);
@@ -63,7 +65,7 @@ struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *ol
6365
if (!(flags & CLONE_NEWUTS))
6466
return old_ns;
6567

66-
new_ns = clone_uts_ns(old_ns);
68+
new_ns = clone_uts_ns(tsk, old_ns);
6769

6870
put_uts_ns(old_ns);
6971
return new_ns;

0 commit comments

Comments
 (0)