linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] Cosmetic
@ 2020-11-21 13:57 Alejandro Colomar
  2020-11-21 13:57 ` [PATCH 1/4] fs/anon_inodes.c: Use "%s" + __func__ instead of hardcoding function name Alejandro Colomar
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Alejandro Colomar @ 2020-11-21 13:57 UTC (permalink / raw)
  To: Alexander Viro, linux-fsdevel; +Cc: Alejandro Colomar, linux-kernel

Hello Alexander,

I passed chackpatch.pl to these files,
and found many things to fix.

I splitted the changes into a few different patches,
to make it easier for review.

Cheers,

Alex


Alejandro Colomar (4):
  fs/anon_inodes.c: Use "%s" + __func__ instead of hardcoding function
    name
  fs/anon_inodes.c, fs/attr.c, fs/bad_inode.c, fs/binfmt_aout.c,
    fs/binfmt_elf.c: Cosmetic
  fs/attr.c, fs/bad_inode.c, fs/binfmt_aout.c, fs/binfmt_elf.c: Cosmetic
  fs/binfmt_elf.c: Cosmetic

 fs/anon_inodes.c |   5 +-
 fs/attr.c        |  12 +--
 fs/bad_inode.c   |  55 +++++-----
 fs/binfmt_aout.c |  95 +++++++++--------
 fs/binfmt_elf.c  | 268 ++++++++++++++++++++++++-----------------------
 5 files changed, 225 insertions(+), 210 deletions(-)

-- 
2.28.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] fs/anon_inodes.c: Use "%s" + __func__ instead of hardcoding function name
  2020-11-21 13:57 [PATCH 0/4] Cosmetic Alejandro Colomar
@ 2020-11-21 13:57 ` Alejandro Colomar
  2020-11-21 13:57 ` [PATCH 2/4] fs/anon_inodes.c, fs/attr.c, fs/bad_inode.c, fs/binfmt_aout.c, fs/binfmt_elf.c: Cosmetic Alejandro Colomar
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2020-11-21 13:57 UTC (permalink / raw)
  To: Alexander Viro, linux-fsdevel; +Cc: Alejandro Colomar, linux-kernel

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---
 fs/anon_inodes.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/anon_inodes.c b/fs/anon_inodes.c
index 89714308c25b..7609d208bb53 100644
--- a/fs/anon_inodes.c
+++ b/fs/anon_inodes.c
@@ -152,11 +152,11 @@ static int __init anon_inode_init(void)
 {
 	anon_inode_mnt = kern_mount(&anon_inode_fs_type);
 	if (IS_ERR(anon_inode_mnt))
-		panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
+		panic("%s() kernel mount failed (%ld)\n", __func__, PTR_ERR(anon_inode_mnt));
 
 	anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
 	if (IS_ERR(anon_inode_inode))
-		panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
+		panic("%s() inode allocation failed (%ld)\n", __func__, PTR_ERR(anon_inode_inode));
 
 	return 0;
 }
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] fs/anon_inodes.c, fs/attr.c, fs/bad_inode.c, fs/binfmt_aout.c, fs/binfmt_elf.c: Cosmetic
  2020-11-21 13:57 [PATCH 0/4] Cosmetic Alejandro Colomar
  2020-11-21 13:57 ` [PATCH 1/4] fs/anon_inodes.c: Use "%s" + __func__ instead of hardcoding function name Alejandro Colomar
@ 2020-11-21 13:57 ` Alejandro Colomar
  2020-11-21 13:57 ` [PATCH 3/4] " Alejandro Colomar
  2020-11-21 13:57 ` [PATCH 4/4] " Alejandro Colomar
  3 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2020-11-21 13:57 UTC (permalink / raw)
  To: Alexander Viro, linux-fsdevel; +Cc: Alejandro Colomar, linux-kernel

This patch contains only trivial changes:
Some of them found with checkpatch.pl in strict mode.

- Remove trailing whitespace.
- Remove spaces coming before tabs.
- Fix typos in comments.
- Convert spaces into tabs.
- Add a space around operators that should have them,
  and remove them when they shouldn't have them sround,
  according to coding_style.rst.
- Use braces accordint to coding_style.rst.
- Align multi-line function prototypes, and other similar cases.
- Remove or add blank lines:
	* Add blank lines after declarations, and before code.
	* Remove blank lines after function definitions and before
	  EXPORT_SYMBOL().
- Remove redundant parentheses, when the don't improve readability.
- Fix comments' style according to coding_style.rst.
- Simplify comparisons against NULL, using '!' (or nothing at all).
- Use C89 comments (/* */), instead of C99 (//).

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---
 fs/anon_inodes.c |   1 +
 fs/attr.c        |   7 +--
 fs/bad_inode.c   |  50 +++++++++---------
 fs/binfmt_aout.c |  94 +++++++++++++++++-----------------
 fs/binfmt_elf.c  | 129 ++++++++++++++++++++++++-----------------------
 5 files changed, 143 insertions(+), 138 deletions(-)

diff --git a/fs/anon_inodes.c b/fs/anon_inodes.c
index 7609d208bb53..bef68dbcbb88 100644
--- a/fs/anon_inodes.c
+++ b/fs/anon_inodes.c
@@ -43,6 +43,7 @@ static const struct dentry_operations anon_inodefs_dentry_operations = {
 static int anon_inodefs_init_fs_context(struct fs_context *fc)
 {
 	struct pseudo_fs_context *ctx = init_pseudo(fc, ANON_INODE_FS_MAGIC);
+
 	if (!ctx)
 		return -ENOMEM;
 	ctx->dops = &anon_inodefs_dentry_operations;
diff --git a/fs/attr.c b/fs/attr.c
index b4bbdbd4c8ca..b32ad8c678a5 100644
--- a/fs/attr.c
+++ b/fs/attr.c
@@ -64,7 +64,7 @@ int setattr_prepare(struct dentry *dentry, struct iattr *attr)
 	unsigned int ia_valid = attr->ia_valid;
 
 	/*
-	 * First check size constraints.  These can't be overriden using
+	 * First check size constraints.  These can't be overridden using
 	 * ATTR_FORCE.
 	 */
 	if (ia_valid & ATTR_SIZE) {
@@ -220,7 +220,8 @@ EXPORT_SYMBOL(setattr_copy);
  * the file open for write, as there can be no conflicting delegation in
  * that case.
  */
-int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
+int notify_change(struct dentry *dentry, struct iattr *attr,
+		  struct inode **delegated_inode)
 {
 	struct inode *inode = dentry->d_inode;
 	umode_t mode = inode->i_mode;
@@ -284,7 +285,7 @@ int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **de
 	 * no function will ever call notify_change with both ATTR_MODE and
 	 * ATTR_KILL_S*ID set.
 	 */
-	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
+	if ((ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) &&
 	    (ia_valid & ATTR_MODE))
 		BUG();
 
diff --git a/fs/bad_inode.c b/fs/bad_inode.c
index 54f0ce444272..f0457b6c17dc 100644
--- a/fs/bad_inode.c
+++ b/fs/bad_inode.c
@@ -22,25 +22,25 @@ static int bad_file_open(struct inode *inode, struct file *filp)
 	return -EIO;
 }
 
-static const struct file_operations bad_file_ops =
-{
+static const struct file_operations bad_file_ops = {
 	.open		= bad_file_open,
 };
 
-static int bad_inode_create (struct inode *dir, struct dentry *dentry,
-		umode_t mode, bool excl)
+static int bad_inode_create(struct inode *dir, struct dentry *dentry,
+			    umode_t mode, bool excl)
 {
 	return -EIO;
 }
 
 static struct dentry *bad_inode_lookup(struct inode *dir,
-			struct dentry *dentry, unsigned int flags)
+				       struct dentry *dentry,
+				       unsigned int flags)
 {
 	return ERR_PTR(-EIO);
 }
 
-static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
-		struct dentry *dentry)
+static int bad_inode_link(struct dentry *old_dentry, struct inode *dir,
+			  struct dentry *dentry)
 {
 	return -EIO;
 }
@@ -50,25 +50,25 @@ static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
 	return -EIO;
 }
 
-static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
-		const char *symname)
+static int bad_inode_symlink(struct inode *dir, struct dentry *dentry,
+			     const char *symname)
 {
 	return -EIO;
 }
 
 static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
-			umode_t mode)
+			   umode_t mode)
 {
 	return -EIO;
 }
 
-static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
+static int bad_inode_rmdir(struct inode *dir, struct dentry *dentry)
 {
 	return -EIO;
 }
 
-static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
-			umode_t mode, dev_t rdev)
+static int bad_inode_mknod(struct inode *dir, struct dentry *dentry,
+			   umode_t mode, dev_t rdev)
 {
 	return -EIO;
 }
@@ -81,7 +81,7 @@ static int bad_inode_rename2(struct inode *old_dir, struct dentry *old_dentry,
 }
 
 static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
-		int buflen)
+			      int buflen)
 {
 	return -EIO;
 }
@@ -103,7 +103,7 @@ static int bad_inode_setattr(struct dentry *direntry, struct iattr *attrs)
 }
 
 static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
-			size_t buffer_size)
+				   size_t buffer_size)
 {
 	return -EIO;
 }
@@ -152,8 +152,7 @@ static int bad_inode_set_acl(struct inode *inode, struct posix_acl *acl,
 	return -EIO;
 }
 
-static const struct inode_operations bad_inode_ops =
-{
+static const struct inode_operations bad_inode_ops = {
 	.create		= bad_inode_create,
 	.lookup		= bad_inode_lookup,
 	.link		= bad_inode_link,
@@ -181,12 +180,12 @@ static const struct inode_operations bad_inode_ops =
 /*
  * When a filesystem is unable to read an inode due to an I/O error in
  * its read_inode() function, it can call make_bad_inode() to return a
- * set of stubs which will return EIO errors as required. 
+ * set of stubs which will return EIO errors as required.
  *
  * We only need to do limited initialisation: all other fields are
  * preinitialised to zero automatically.
  */
- 
+
 /**
  *	make_bad_inode - mark an inode bad due to an I/O error
  *	@inode: Inode to mark bad
@@ -195,7 +194,7 @@ static const struct inode_operations bad_inode_ops =
  *	failure this function makes the inode "bad" and causes I/O operations
  *	on it to fail from this point on.
  */
- 
+
 void make_bad_inode(struct inode *inode)
 {
 	remove_inode_hash(inode);
@@ -203,9 +202,9 @@ void make_bad_inode(struct inode *inode)
 	inode->i_mode = S_IFREG;
 	inode->i_atime = inode->i_mtime = inode->i_ctime =
 		current_time(inode);
-	inode->i_op = &bad_inode_ops;	
+	inode->i_op = &bad_inode_ops;
 	inode->i_opflags &= ~IOP_XATTR;
-	inode->i_fop = &bad_file_ops;	
+	inode->i_fop = &bad_file_ops;
 }
 EXPORT_SYMBOL(make_bad_inode);
 
@@ -214,19 +213,18 @@ EXPORT_SYMBOL(make_bad_inode);
  * &bad_inode_ops to cover the case of invalidated inodes as well as
  * those created by make_bad_inode() above.
  */
- 
+
 /**
  *	is_bad_inode - is an inode errored
  *	@inode: inode to test
  *
  *	Returns true if the inode in question has been marked as bad.
  */
- 
+
 bool is_bad_inode(struct inode *inode)
 {
-	return (inode->i_op == &bad_inode_ops);	
+	return (inode->i_op == &bad_inode_ops);
 }
-
 EXPORT_SYMBOL(is_bad_inode);
 
 /**
diff --git a/fs/binfmt_aout.c b/fs/binfmt_aout.c
index 3e84e9bb9084..92d6b70ddab0 100644
--- a/fs/binfmt_aout.c
+++ b/fs/binfmt_aout.c
@@ -32,7 +32,7 @@
 #include <asm/cacheflush.h>
 
 static int load_aout_binary(struct linux_binprm *);
-static int load_aout_library(struct file*);
+static int load_aout_library(struct file *);
 
 static struct linux_binfmt aout_format = {
 	.module		= THIS_MODULE,
@@ -56,7 +56,7 @@ static int set_brk(unsigned long start, unsigned long end)
  * memory and creates the pointer tables from them, and puts their
  * addresses on the "stack", returning the new stack pointer value.
  */
-static unsigned long __user *create_aout_tables(char __user *p, struct linux_binprm * bprm)
+static unsigned long __user *create_aout_tables(char __user *p, struct linux_binprm *bprm)
 {
 	char __user * __user *argv;
 	char __user * __user *envp;
@@ -64,7 +64,7 @@ static unsigned long __user *create_aout_tables(char __user *p, struct linux_bin
 	int argc = bprm->argc;
 	int envc = bprm->envc;
 
-	sp = (void __user *)((-(unsigned long)sizeof(char *)) & (unsigned long) p);
+	sp = (void __user *)((-(unsigned long)sizeof(char *)) & (unsigned long)p);
 #ifdef __alpha__
 /* whee.. test-programs are so much fun. */
 	put_user(0, --sp);
@@ -78,34 +78,36 @@ static unsigned long __user *create_aout_tables(char __user *p, struct linux_bin
 	put_user(bprm->exec, --sp);
 	put_user(1001, --sp);
 #endif
-	sp -= envc+1;
+	sp -= envc + 1;
 	envp = (char __user * __user *) sp;
-	sp -= argc+1;
+	sp -= argc + 1;
 	argv = (char __user * __user *) sp;
 #ifndef __alpha__
-	put_user((unsigned long) envp,--sp);
-	put_user((unsigned long) argv,--sp);
+	put_user((unsigned long)envp, --sp);
+	put_user((unsigned long)argv, --sp);
 #endif
-	put_user(argc,--sp);
-	current->mm->arg_start = (unsigned long) p;
-	while (argc-->0) {
+	put_user(argc, --sp);
+	current->mm->arg_start = (unsigned long)p;
+	while (argc-- > 0) {
 		char c;
-		put_user(p,argv++);
+
+		put_user(p, argv++);
 		do {
-			get_user(c,p++);
+			get_user(c, p++);
 		} while (c);
 	}
-	put_user(NULL,argv);
-	current->mm->arg_end = current->mm->env_start = (unsigned long) p;
-	while (envc-->0) {
+	put_user(NULL, argv);
+	current->mm->arg_end = current->mm->env_start = (unsigned long)p;
+	while (envc-- > 0) {
 		char c;
-		put_user(p,envp++);
+
+		put_user(p, envp++);
 		do {
-			get_user(c,p++);
+			get_user(c, p++);
 		} while (c);
 	}
-	put_user(NULL,envp);
-	current->mm->env_end = (unsigned long) p;
+	put_user(NULL, envp);
+	current->mm->env_end = (unsigned long)p;
 	return sp;
 }
 
@@ -114,7 +116,7 @@ static unsigned long __user *create_aout_tables(char __user *p, struct linux_bin
  * libraries.  There is no binary dependent code anywhere else.
  */
 
-static int load_aout_binary(struct linux_binprm * bprm)
+static int load_aout_binary(struct linux_binprm *bprm)
 {
 	struct pt_regs *regs = current_pt_regs();
 	struct exec ex;
@@ -123,11 +125,14 @@ static int load_aout_binary(struct linux_binprm * bprm)
 	unsigned long rlim;
 	int retval;
 
-	ex = *((struct exec *) bprm->buf);		/* exec-header */
+	ex = *(struct exec *)bprm->buf;		/* exec-header */
 	if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
 	     N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
 	    N_TRSIZE(ex) || N_DRSIZE(ex) ||
-	    i_size_read(file_inode(bprm->file)) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
+	    i_size_read(file_inode(bprm->file)) < ex.a_text +
+						  ex.a_data +
+						  N_SYMSIZE(ex) +
+						  N_TXTOFF(ex)) {
 		return -ENOEXEC;
 	}
 
@@ -174,7 +179,6 @@ static int load_aout_binary(struct linux_binprm * bprm)
 	if (retval < 0)
 		return retval;
 
-
 	if (N_MAGIC(ex) == OMAGIC) {
 		unsigned long text_addr, map_size;
 		loff_t pos;
@@ -183,35 +187,34 @@ static int load_aout_binary(struct linux_binprm * bprm)
 
 #ifdef __alpha__
 		pos = fd_offset;
-		map_size = ex.a_text+ex.a_data + PAGE_SIZE - 1;
+		map_size = ex.a_text + ex.a_data + PAGE_SIZE - 1;
 #else
 		pos = 32;
-		map_size = ex.a_text+ex.a_data;
+		map_size = ex.a_text + ex.a_data;
 #endif
 		error = vm_brk(text_addr & PAGE_MASK, map_size);
 		if (error)
 			return error;
 
 		error = read_code(bprm->file, text_addr, pos,
-				  ex.a_text+ex.a_data);
+				  ex.a_text + ex.a_data);
 		if ((signed long)error < 0)
 			return error;
 	} else {
 		if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
-		    (N_MAGIC(ex) != NMAGIC) && printk_ratelimit())
-		{
+				(N_MAGIC(ex) != NMAGIC) &&
+				printk_ratelimit()) {
 			printk(KERN_NOTICE "executable not page aligned\n");
 		}
 
-		if ((fd_offset & ~PAGE_MASK) != 0 && printk_ratelimit())
-		{
-			printk(KERN_WARNING 
+		if ((fd_offset & ~PAGE_MASK) != 0 && printk_ratelimit()) {
+			printk(KERN_WARNING
 			       "fd_offset is not page aligned. Please convert program: %pD\n",
 			       bprm->file);
 		}
 
-		if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
-			error = vm_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
+		if (!bprm->file->f_op->mmap || ((fd_offset & ~PAGE_MASK) != 0)) {
+			error = vm_brk(N_TXTADDR(ex), ex.a_text + ex.a_data);
 			if (error)
 				return error;
 
@@ -221,9 +224,9 @@ static int load_aout_binary(struct linux_binprm * bprm)
 		}
 
 		error = vm_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
-			PROT_READ | PROT_EXEC,
-			MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
-			fd_offset);
+				PROT_READ | PROT_EXEC,
+				MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
+				fd_offset);
 
 		if (error != N_TXTADDR(ex))
 			return error;
@@ -243,7 +246,7 @@ static int load_aout_binary(struct linux_binprm * bprm)
 		return retval;
 
 	current->mm->start_stack =
-		(unsigned long) create_aout_tables((char __user *) bprm->p, bprm);
+		(unsigned long)create_aout_tables((char __user *)bprm->p, bprm);
 #ifdef __alpha__
 	regs->gp = ex.a_gpvalue;
 #endif
@@ -254,7 +257,7 @@ static int load_aout_binary(struct linux_binprm * bprm)
 
 static int load_aout_library(struct file *file)
 {
-	struct inode * inode;
+	struct inode *inode;
 	unsigned long bss, start_addr, len;
 	unsigned long error;
 	int retval;
@@ -271,7 +274,8 @@ static int load_aout_library(struct file *file)
 	/* We come in here for the regular a.out style of shared libraries */
 	if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
 	    N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
-	    i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
+	    i_size_read(inode) < ex.a_text + ex.a_data + N_SYMSIZE(ex) +
+				 N_TXTOFF(ex)) {
 		goto out;
 	}
 
@@ -285,15 +289,15 @@ static int load_aout_library(struct file *file)
 	if (N_FLAGS(ex))
 		goto out;
 
-	/* For  QMAGIC, the starting address is 0x20 into the page.  We mask
-	   this off to get the starting address for the page */
-
+	/*
+	 * For  QMAGIC, the starting address is 0x20 into the page.
+	 * We mask this off to get the starting address for the page
+	 */
 	start_addr =  ex.a_entry & 0xfffff000;
 
 	if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
-		if (printk_ratelimit())
-		{
-			printk(KERN_WARNING 
+		if (printk_ratelimit()) {
+			printk(KERN_WARNING
 			       "N_TXTOFF is not page aligned. Please convert library: %pD\n",
 			       file);
 		}
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index fa50e8936f5f..955927ac2b80 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1025,10 +1025,12 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	start_data = 0;
 	end_data = 0;
 
-	/* Now we do a little grungy work by mmapping the ELF image into
-	   the correct location in memory. */
-	for(i = 0, elf_ppnt = elf_phdata;
-	    i < elf_ex->e_phnum; i++, elf_ppnt++) {
+	/*
+	 * Now we do a little grungy work by mmapping the ELF image into
+	 * the correct location in memory.
+	 */
+	for (i = 0, elf_ppnt = elf_phdata; i < elf_ex->e_phnum;
+					   i++, elf_ppnt++) {
 		int elf_prot, elf_flags;
 		unsigned long k, vaddr;
 		unsigned long total_size = 0;
@@ -1037,12 +1039,14 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		if (elf_ppnt->p_type != PT_LOAD)
 			continue;
 
-		if (unlikely (elf_brk > elf_bss)) {
+		if (unlikely(elf_brk > elf_bss)) {
 			unsigned long nbyte;
-	            
-			/* There was a PT_LOAD segment with p_memsz > p_filesz
-			   before this one. Map anonymous pages, if needed,
-			   and clear the area.  */
+
+			/*
+			 * There was a PT_LOAD segment with p_memsz > p_filesz
+			 * before this one. Map anonymous pages, if needed,
+			 * and clear the area.
+			 */
 			retval = set_brk(elf_bss + load_bias,
 					 elf_brk + load_bias,
 					 bss_prot);
@@ -1139,7 +1143,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 				elf_prot, elf_flags, total_size);
 		if (BAD_ADDR(error)) {
 			retval = IS_ERR((void *)error) ?
-				PTR_ERR((void*)error) : -EINVAL;
+				PTR_ERR((void *)error) : -EINVAL;
 			goto out_free_dentry;
 		}
 
@@ -1252,7 +1256,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
 
 	retval = create_elf_tables(bprm, elf_ex,
-			  load_addr, interp_load_addr, e_entry);
+				   load_addr, interp_load_addr, e_entry);
 	if (retval < 0)
 		goto out;
 
@@ -1283,10 +1287,12 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	}
 
 	if (current->personality & MMAP_PAGE_ZERO) {
-		/* Why this, you ask???  Well SVr4 maps page 0 as read-only,
-		   and some applications "depend" upon this behavior.
-		   Since we do not have the power to recompile these, we
-		   emulate the SVr4 behavior. Sigh. */
+		/*
+		 * Why this, you ask???  Well SVr4 maps page 0 as read-only,
+		 * and some applications "depend" upon this behavior.
+		 * Since we do not have the power to recompile these, we
+		 * emulate the SVr4 behavior. Sigh.
+		 */
 		error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
 				MAP_FIXED | MAP_PRIVATE, 0);
 	}
@@ -1325,8 +1331,10 @@ static int load_elf_binary(struct linux_binprm *bprm)
 }
 
 #ifdef CONFIG_USELIB
-/* This is really simpleminded and specialized - we are loading an
-   a.out library that is given an ELF header. */
+/*
+ * This is really simpleminded and specialized - we are loading an
+ * a.out library that is given an ELF header.
+ */
 static int load_elf_library(struct file *file)
 {
 	struct elf_phdr *elf_phdata;
@@ -1366,7 +1374,7 @@ static int load_elf_library(struct file *file)
 	if (retval < 0)
 		goto out_free_ph;
 
-	for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
+	for (j = 0, i = 0; i < elf_ex.e_phnum; i++)
 		if ((eppnt + i)->p_type == PT_LOAD)
 			j++;
 	if (j != 1)
@@ -1418,8 +1426,7 @@ static int load_elf_library(struct file *file)
  */
 
 /* An ELF note in memory */
-struct memelfnote
-{
+struct memelfnote {
 	const char *name;
 	int type;
 	unsigned int datasz;
@@ -1440,6 +1447,7 @@ static int notesize(struct memelfnote *en)
 static int writenote(struct memelfnote *men, struct coredump_params *cprm)
 {
 	struct elf_note en;
+
 	en.n_namesz = strlen(men->name) + 1;
 	en.n_descsz = men->datasz;
 	en.n_type = men->type;
@@ -1482,8 +1490,8 @@ static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
 	phdr->p_align = 0;
 }
 
-static void fill_note(struct memelfnote *note, const char *name, int type, 
-		unsigned int sz, void *data)
+static void fill_note(struct memelfnote *note, const char *name, int type,
+		      unsigned int sz, void *data)
 {
 	note->name = name;
 	note->type = type;
@@ -1496,7 +1504,7 @@ static void fill_note(struct memelfnote *note, const char *name, int type,
  * registers which need to be filled up separately.
  */
 static void fill_prstatus(struct elf_prstatus *prstatus,
-		struct task_struct *p, long signr)
+			  struct task_struct *p, long signr)
 {
 	prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
 	prstatus->pr_sigpend = p->pending.signal.sig[0];
@@ -1534,17 +1542,17 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
 {
 	const struct cred *cred;
 	unsigned int i, len;
-	
+
 	/* first copy the parameters from user space */
 	memset(psinfo, 0, sizeof(struct elf_prpsinfo));
 
 	len = mm->arg_end - mm->arg_start;
 	if (len >= ELF_PRARGSZ)
-		len = ELF_PRARGSZ-1;
+		len = ELF_PRARGSZ - 1;
 	if (copy_from_user(&psinfo->pr_psargs,
-		           (const char __user *)mm->arg_start, len))
+			   (const char __user *)mm->arg_start, len))
 		return -EFAULT;
-	for(i = 0; i < len; i++)
+	for (i = 0; i < len; i++)
 		if (psinfo->pr_psargs[i] == 0)
 			psinfo->pr_psargs[i] = ' ';
 	psinfo->pr_psargs[len] = 0;
@@ -1568,14 +1576,15 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
 	SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
 	rcu_read_unlock();
 	strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
-	
+
 	return 0;
 }
 
 static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
 {
-	elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv;
+	elf_addr_t *auxv = (elf_addr_t *)mm->saved_auxv;
 	int i = 0;
+
 	do
 		i += 2;
 	while (auxv[i - 2] != AT_NULL);
@@ -1583,13 +1592,13 @@ static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
 }
 
 static void fill_siginfo_note(struct memelfnote *note, user_siginfo_t *csigdata,
-		const kernel_siginfo_t *siginfo)
+			      const kernel_siginfo_t *siginfo)
 {
 	copy_siginfo_to_external(csigdata, siginfo);
 	fill_note(note, "CORE", NT_SIGINFO, sizeof(*csigdata), csigdata);
 }
 
-#define MAX_FILE_NOTE_SIZE (4*1024*1024)
+#define MAX_FILE_NOTE_SIZE (4 * 1024 * 1024)
 /*
  * Format of NT_FILE note:
  *
@@ -1605,7 +1614,7 @@ static int fill_files_note(struct memelfnote *note)
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
-	unsigned count, size, names_ofs, remaining, n;
+	unsigned int count, size, names_ofs, remaining, n;
 	user_long_t *data;
 	user_long_t *start_end_ofs;
 	char *name_base, *name_curpos;
@@ -1633,7 +1642,7 @@ static int fill_files_note(struct memelfnote *note)
 	name_base = name_curpos = ((char *)data) + names_ofs;
 	remaining = size - names_ofs;
 	count = 0;
-	for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
+	for (vma = mm->mmap; vma; vma = vma->vm_next) {
 		struct file *file;
 		const char *filename;
 
@@ -1739,7 +1748,8 @@ static int fill_thread_core_info(struct elf_thread_core_info *t,
 	 */
 	fill_prstatus(&t->prstatus, t->task, signr);
 	regset0_size = regset_get(t->task, &view->regsets[0],
-		   sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg);
+				  sizeof(t->prstatus.pr_reg),
+				  &t->prstatus.pr_reg);
 	if (regset0_size < 0)
 		return 0;
 
@@ -1762,7 +1772,7 @@ static int fill_thread_core_info(struct elf_thread_core_info *t,
 		int ret;
 
 		do_thread_regset_writeback(t->task, regset);
-		if (!note_type) // not for coredumps
+		if (!note_type) /* not for coredumps */
 			continue;
 		if (regset->active && regset->active(t->task, regset) <= 0)
 			continue;
@@ -1798,16 +1808,14 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
 	info->thread = NULL;
 
 	psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
-	if (psinfo == NULL) {
+	if (!psinfo) {
 		info->psinfo.data = NULL; /* So we don't free this wrongly */
 		return 0;
 	}
 
 	fill_note(&info->psinfo, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
 
-	/*
-	 * Figure out how many notes we're going to need for each thread.
-	 */
+	/* Figure out how many notes we're going to need for each thread. */
 	info->thread_notes = 0;
 	for (i = 0; i < view->n; ++i)
 		if (view->regsets[i].core_note_type != 0)
@@ -1823,15 +1831,11 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
 		return 0;
 	}
 
-	/*
-	 * Initialize the ELF file header.
-	 */
+	/* Initialize the ELF file header. */
 	fill_elf_header(elf, phdrs,
 			view->e_machine, view->e_flags);
 
-	/*
-	 * Allocate a structure for each thread.
-	 */
+	/* Allocate a structure for each thread. */
 	for (ct = &dump_task->mm->core_state->dumper; ct; ct = ct->next) {
 		t = kzalloc(offsetof(struct elf_thread_core_info,
 				     notes[info->thread_notes]),
@@ -1853,16 +1857,12 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
 		}
 	}
 
-	/*
-	 * Now fill in each thread's information.
-	 */
-	for (t = info->thread; t != NULL; t = t->next)
+	/* Now fill in each thread's information. */
+	for (t = info->thread; t; t = t->next)
 		if (!fill_thread_core_info(t, view, siginfo->si_signo, &info->size))
 			return 0;
 
-	/*
-	 * Fill in the two process-wide notes.
-	 */
+	/* Fill in the two process-wide notes. */
 	fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm);
 	info->size += notesize(&info->psinfo);
 
@@ -1905,8 +1905,7 @@ static int write_note_info(struct elf_note_info *info,
 			return 0;
 		if (first && !writenote(&info->auxv, cprm))
 			return 0;
-		if (first && info->files.data &&
-				!writenote(&info->files, cprm))
+		if (first && info->files.data && !writenote(&info->files, cprm))
 			return 0;
 
 		for (i = 1; i < info->thread_notes; ++i)
@@ -1924,6 +1923,7 @@ static int write_note_info(struct elf_note_info *info,
 static void free_note_info(struct elf_note_info *info)
 {
 	struct elf_thread_core_info *threads = info->thread;
+
 	while (threads) {
 		unsigned int i;
 		struct elf_thread_core_info *t = threads;
@@ -1940,8 +1940,7 @@ static void free_note_info(struct elf_note_info *info)
 #else
 
 /* Here is the structure in which status of each thread is captured. */
-struct elf_thread_status
-{
+struct elf_thread_status {
 	struct list_head list;
 	struct elf_prstatus prstatus;	/* NT_PRSTATUS */
 	elf_fpregset_t fpu;		/* NT_PRFPREG */
@@ -1959,20 +1958,21 @@ static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
 {
 	int sz = 0;
 	struct task_struct *p = t->thread;
+
 	t->num_notes = 0;
 
 	fill_prstatus(&t->prstatus, p, signr);
-	elf_core_copy_task_regs(p, &t->prstatus.pr_reg);	
-	
+	elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
+
 	fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
-		  &(t->prstatus));
+		  &t->prstatus);
 	t->num_notes++;
 	sz += notesize(&t->notes[0]);
 
-	if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
-								&t->fpu))) {
+	t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu);
+	if (t->prstatus.pr_fpvalid) {
 		fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
-			  &(t->fpu));
+			  &t->fpu);
 		t->num_notes++;
 		sz += notesize(&t->notes[1]);
 	}
@@ -2179,7 +2179,8 @@ static int elf_core_dump(struct coredump_params *cprm)
 
 	/* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
 	 * this, kernel supports extended numbering. Have a look at
-	 * include/linux/elf.h for further information. */
+	 * include/linux/elf.h for further information.
+	 */
 	e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
 
 	/*
@@ -2257,7 +2258,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 	if (!elf_core_write_extra_phdrs(cprm, offset))
 		goto end_coredump;
 
- 	/* write out the notes section */
+	/* write out the notes section */
 	if (!write_note_info(&info, cprm))
 		goto end_coredump;
 
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] fs/attr.c, fs/bad_inode.c, fs/binfmt_aout.c, fs/binfmt_elf.c: Cosmetic
  2020-11-21 13:57 [PATCH 0/4] Cosmetic Alejandro Colomar
  2020-11-21 13:57 ` [PATCH 1/4] fs/anon_inodes.c: Use "%s" + __func__ instead of hardcoding function name Alejandro Colomar
  2020-11-21 13:57 ` [PATCH 2/4] fs/anon_inodes.c, fs/attr.c, fs/bad_inode.c, fs/binfmt_aout.c, fs/binfmt_elf.c: Cosmetic Alejandro Colomar
@ 2020-11-21 13:57 ` Alejandro Colomar
  2020-11-21 13:57 ` [PATCH 4/4] " Alejandro Colomar
  3 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2020-11-21 13:57 UTC (permalink / raw)
  To: Alexander Viro, linux-fsdevel; +Cc: Alejandro Colomar, linux-kernel

Slightly non-trivial changes:

- Move declarations to the top of function definitions.
- Split multiple assignments in a single line to
  multiple lines with a signle assignment each.

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---
 fs/attr.c        |  5 ++---
 fs/bad_inode.c   |  5 +++--
 fs/binfmt_aout.c |  3 ++-
 fs/binfmt_elf.c  | 26 ++++++++++++++++----------
 4 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/fs/attr.c b/fs/attr.c
index b32ad8c678a5..61f7a75ac330 100644
--- a/fs/attr.c
+++ b/fs/attr.c
@@ -62,13 +62,14 @@ int setattr_prepare(struct dentry *dentry, struct iattr *attr)
 {
 	struct inode *inode = d_inode(dentry);
 	unsigned int ia_valid = attr->ia_valid;
+	int error;
 
 	/*
 	 * First check size constraints.  These can't be overridden using
 	 * ATTR_FORCE.
 	 */
 	if (ia_valid & ATTR_SIZE) {
-		int error = inode_newsize_ok(inode, attr->ia_size);
+		error = inode_newsize_ok(inode, attr->ia_size);
 		if (error)
 			return error;
 	}
@@ -105,8 +106,6 @@ int setattr_prepare(struct dentry *dentry, struct iattr *attr)
 kill_priv:
 	/* User has permission for the change */
 	if (ia_valid & ATTR_KILL_PRIV) {
-		int error;
-
 		error = security_inode_killpriv(dentry);
 		if (error)
 			return error;
diff --git a/fs/bad_inode.c b/fs/bad_inode.c
index f0457b6c17dc..4c5e677ec423 100644
--- a/fs/bad_inode.c
+++ b/fs/bad_inode.c
@@ -200,8 +200,9 @@ void make_bad_inode(struct inode *inode)
 	remove_inode_hash(inode);
 
 	inode->i_mode = S_IFREG;
-	inode->i_atime = inode->i_mtime = inode->i_ctime =
-		current_time(inode);
+	inode->i_ctime = current_time(inode);
+	inode->i_mtime = inode->i_ctime;
+	inode->i_atime = inode->i_ctime;
 	inode->i_op = &bad_inode_ops;
 	inode->i_opflags &= ~IOP_XATTR;
 	inode->i_fop = &bad_file_ops;
diff --git a/fs/binfmt_aout.c b/fs/binfmt_aout.c
index 92d6b70ddab0..976d5f1565e1 100644
--- a/fs/binfmt_aout.c
+++ b/fs/binfmt_aout.c
@@ -97,7 +97,8 @@ static unsigned long __user *create_aout_tables(char __user *p, struct linux_bin
 		} while (c);
 	}
 	put_user(NULL, argv);
-	current->mm->arg_end = current->mm->env_start = (unsigned long)p;
+	current->mm->env_start = (unsigned long)p;
+	current->mm->arg_end = (unsigned long)p;
 	while (envc-- > 0) {
 		char c;
 
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 955927ac2b80..b5e1e0a0917a 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1035,13 +1035,12 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		unsigned long k, vaddr;
 		unsigned long total_size = 0;
 		unsigned long alignment;
+		unsigned long nbyte;
 
 		if (elf_ppnt->p_type != PT_LOAD)
 			continue;
 
 		if (unlikely(elf_brk > elf_bss)) {
-			unsigned long nbyte;
-
 			/*
 			 * There was a PT_LOAD segment with p_memsz > p_filesz
 			 * before this one. Map anonymous pages, if needed,
@@ -1277,10 +1276,12 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		 */
 		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
 		    elf_ex->e_type == ET_DYN && !interpreter) {
-			mm->brk = mm->start_brk = ELF_ET_DYN_BASE;
+			mm->start_brk	= ELF_ET_DYN_BASE;
+			mm->brk		= ELF_ET_DYN_BASE;
 		}
 
-		mm->brk = mm->start_brk = arch_randomize_brk(mm);
+		mm->start_brk = arch_randomize_brk(mm);
+		mm->brk = mm->start_brk;
 #ifdef compat_brk_randomized
 		current->brk_randomized = 1;
 #endif
@@ -1506,7 +1507,8 @@ static void fill_note(struct memelfnote *note, const char *name, int type,
 static void fill_prstatus(struct elf_prstatus *prstatus,
 			  struct task_struct *p, long signr)
 {
-	prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
+	prstatus->pr_cursig = signr;
+	prstatus->pr_info.si_signo = signr;
 	prstatus->pr_sigpend = p->pending.signal.sig[0];
 	prstatus->pr_sighold = p->blocked.sig[0];
 	rcu_read_lock();
@@ -1618,6 +1620,7 @@ static int fill_files_note(struct memelfnote *note)
 	user_long_t *data;
 	user_long_t *start_end_ofs;
 	char *name_base, *name_curpos;
+	unsigned int shift_bytes;
 
 	/* *Estimated* file count and total data size needed */
 	count = mm->map_count;
@@ -1639,7 +1642,8 @@ static int fill_files_note(struct memelfnote *note)
 		return -ENOMEM;
 
 	start_end_ofs = data + 2;
-	name_base = name_curpos = ((char *)data) + names_ofs;
+	name_curpos = ((char *)data) + names_ofs;
+	name_base = name_curpos;
 	remaining = size - names_ofs;
 	count = 0;
 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
@@ -1681,7 +1685,7 @@ static int fill_files_note(struct memelfnote *note)
 	 */
 	n = mm->map_count - count;
 	if (n != 0) {
-		unsigned shift_bytes = n * 3 * sizeof(data[0]);
+		shift_bytes = n * 3 * sizeof(data[0]);
 		memmove(name_base - shift_bytes, name_base,
 			name_curpos - name_base);
 		name_curpos -= shift_bytes;
@@ -1922,11 +1926,12 @@ static int write_note_info(struct elf_note_info *info,
 
 static void free_note_info(struct elf_note_info *info)
 {
+	struct elf_thread_core_info *t;
 	struct elf_thread_core_info *threads = info->thread;
+	unsigned int i;
 
 	while (threads) {
-		unsigned int i;
-		struct elf_thread_core_info *t = threads;
+		t = threads;
 		threads = t->next;
 		WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus);
 		for (i = 1; i < info->thread_notes; ++i)
@@ -2209,7 +2214,8 @@ static int elf_core_dump(struct coredump_params *cprm)
 		offset += sz;
 	}
 
-	dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
+	offset = roundup(offset, ELF_EXEC_PAGESIZE);
+	dataoff = offset;
 
 	offset += vma_data_size;
 	offset += elf_core_extra_data_size();
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] fs/binfmt_elf.c: Cosmetic
  2020-11-21 13:57 [PATCH 0/4] Cosmetic Alejandro Colomar
                   ` (2 preceding siblings ...)
  2020-11-21 13:57 ` [PATCH 3/4] " Alejandro Colomar
@ 2020-11-21 13:57 ` Alejandro Colomar
  3 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2020-11-21 13:57 UTC (permalink / raw)
  To: Alexander Viro, linux-fsdevel; +Cc: Alejandro Colomar, linux-kernel

Non-trivial changes:

Invert 'if's to simplify logic.
Use 'goto' in conjunction with the above, when appropriate.

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---
 fs/binfmt_elf.c | 115 +++++++++++++++++++++++++-----------------------
 1 file changed, 59 insertions(+), 56 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index b5e1e0a0917a..dbd50b5bf238 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1079,65 +1079,68 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		 */
 		if (elf_ex->e_type == ET_EXEC || load_addr_set) {
 			elf_flags |= MAP_FIXED;
-		} else if (elf_ex->e_type == ET_DYN) {
-			/*
-			 * This logic is run once for the first LOAD Program
-			 * Header for ET_DYN binaries to calculate the
-			 * randomization (load_bias) for all the LOAD
-			 * Program Headers, and to calculate the entire
-			 * size of the ELF mapping (total_size). (Note that
-			 * load_addr_set is set to true later once the
-			 * initial mapping is performed.)
-			 *
-			 * There are effectively two types of ET_DYN
-			 * binaries: programs (i.e. PIE: ET_DYN with INTERP)
-			 * and loaders (ET_DYN without INTERP, since they
-			 * _are_ the ELF interpreter). The loaders must
-			 * be loaded away from programs since the program
-			 * may otherwise collide with the loader (especially
-			 * for ET_EXEC which does not have a randomized
-			 * position). For example to handle invocations of
-			 * "./ld.so someprog" to test out a new version of
-			 * the loader, the subsequent program that the
-			 * loader loads must avoid the loader itself, so
-			 * they cannot share the same load range. Sufficient
-			 * room for the brk must be allocated with the
-			 * loader as well, since brk must be available with
-			 * the loader.
-			 *
-			 * Therefore, programs are loaded offset from
-			 * ELF_ET_DYN_BASE and loaders are loaded into the
-			 * independently randomized mmap region (0 load_bias
-			 * without MAP_FIXED).
-			 */
-			if (interpreter) {
-				load_bias = ELF_ET_DYN_BASE;
-				if (current->flags & PF_RANDOMIZE)
-					load_bias += arch_mmap_rnd();
-				alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum);
-				if (alignment)
-					load_bias &= ~(alignment - 1);
-				elf_flags |= MAP_FIXED;
-			} else
-				load_bias = 0;
+			goto proceed_normally;
+		}
+		if (elf_ex->e_type != ET_DYN)
+			goto proceed_normally;
+		/*
+		 * This logic is run once for the first LOAD Program
+		 * Header for ET_DYN binaries to calculate the
+		 * randomization (load_bias) for all the LOAD
+		 * Program Headers, and to calculate the entire
+		 * size of the ELF mapping (total_size). (Note that
+		 * load_addr_set is set to true later once the
+		 * initial mapping is performed.)
+		 *
+		 * There are effectively two types of ET_DYN
+		 * binaries: programs (i.e. PIE: ET_DYN with INTERP)
+		 * and loaders (ET_DYN without INTERP, since they
+		 * _are_ the ELF interpreter). The loaders must
+		 * be loaded away from programs since the program
+		 * may otherwise collide with the loader (especially
+		 * for ET_EXEC which does not have a randomized
+		 * position). For example to handle invocations of
+		 * "./ld.so someprog" to test out a new version of
+		 * the loader, the subsequent program that the
+		 * loader loads must avoid the loader itself, so
+		 * they cannot share the same load range. Sufficient
+		 * room for the brk must be allocated with the
+		 * loader as well, since brk must be available with
+		 * the loader.
+		 *
+		 * Therefore, programs are loaded offset from
+		 * ELF_ET_DYN_BASE and loaders are loaded into the
+		 * independently randomized mmap region (0 load_bias
+		 * without MAP_FIXED).
+		 */
+		if (interpreter) {
+			load_bias = ELF_ET_DYN_BASE;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias += arch_mmap_rnd();
+			alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum);
+			if (alignment)
+				load_bias &= ~(alignment - 1);
+			elf_flags |= MAP_FIXED;
+		} else {
+			load_bias = 0;
+		}
 
-			/*
-			 * Since load_bias is used for all subsequent loading
-			 * calculations, we must lower it by the first vaddr
-			 * so that the remaining calculations based on the
-			 * ELF vaddrs will be correctly offset. The result
-			 * is then page aligned.
-			 */
-			load_bias = ELF_PAGESTART(load_bias - vaddr);
+		/*
+		 * Since load_bias is used for all subsequent loading
+		 * calculations, we must lower it by the first vaddr
+		 * so that the remaining calculations based on the
+		 * ELF vaddrs will be correctly offset. The result
+		 * is then page aligned.
+		 */
+		load_bias = ELF_PAGESTART(load_bias - vaddr);
 
-			total_size = total_mapping_size(elf_phdata,
-							elf_ex->e_phnum);
-			if (!total_size) {
-				retval = -EINVAL;
-				goto out_free_dentry;
-			}
+		total_size = total_mapping_size(elf_phdata,
+						elf_ex->e_phnum);
+		if (!total_size) {
+			retval = -EINVAL;
+			goto out_free_dentry;
 		}
-
+proceed_normally:	/* FIXME: a better label name? */
 		error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
 				elf_prot, elf_flags, total_size);
 		if (BAD_ADDR(error)) {
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-11-21 13:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-21 13:57 [PATCH 0/4] Cosmetic Alejandro Colomar
2020-11-21 13:57 ` [PATCH 1/4] fs/anon_inodes.c: Use "%s" + __func__ instead of hardcoding function name Alejandro Colomar
2020-11-21 13:57 ` [PATCH 2/4] fs/anon_inodes.c, fs/attr.c, fs/bad_inode.c, fs/binfmt_aout.c, fs/binfmt_elf.c: Cosmetic Alejandro Colomar
2020-11-21 13:57 ` [PATCH 3/4] " Alejandro Colomar
2020-11-21 13:57 ` [PATCH 4/4] " Alejandro Colomar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).