mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Philippe Mikoyan <philippe.mikoyan@skat.systems>
To: akpm@linux-foundation.org
Cc: viro@zeniv.linux.org.uk, manfred@colorfullife.com,
	linux-kernel@vger.kernel.org, edgar.kaziakhmedov@virtuozzo.com,
	philippe.mikoyan@skat.systems
Subject: [PATCH 1/2] ipc/shm: Fix shm_nattch incorrect value
Date: Thu, 30 Nov 2017 09:12:23 +0300	[thread overview]
Message-ID: <20171130061224.25466-2-philippe.mikoyan@skat.systems> (raw)
In-Reply-To: <20171130061224.25466-1-philippe.mikoyan@skat.systems>

This patch fixes that do_shmat increases shm_nattch value twice.

E.g. if memory segment was created just now and process attaches it,
shmctl(..IPC_STAT..) of concurrently running process can at some
point of time return data structure with 'shm_nattch' equal to 2.

Signed-off-by: Philippe Mikoyan <philippe.mikoyan@skat.systems>
Signed-off-by: Edgar Kaziakhmedov <edgar.kaziakhmedov@virtuozzo.com>
---
 ipc/shm.c | 58 +++++++++++++++++++++++++++-------------------------------
 1 file changed, 27 insertions(+), 31 deletions(-)

diff --git a/ipc/shm.c b/ipc/shm.c
index badac463e2c8..565f17925128 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -190,33 +190,31 @@ static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
 	ipc_rmid(&shm_ids(ns), &s->shm_perm);
 }

-
-static int __shm_open(struct vm_area_struct *vma)
-{
-	struct file *file = vma->vm_file;
-	struct shm_file_data *sfd = shm_file_data(file);
-	struct shmid_kernel *shp;
-
-	shp = shm_lock(sfd->ns, sfd->id);
-
-	if (IS_ERR(shp))
-		return PTR_ERR(shp);
-
-	shp->shm_atim = ktime_get_real_seconds();
-	shp->shm_lprid = task_tgid_vnr(current);
-	shp->shm_nattch++;
-	shm_unlock(shp);
-	return 0;
-}
-
 /* This is called by fork, once for every shm attach. */
 static void shm_open(struct vm_area_struct *vma)
 {
-	int err = __shm_open(vma);
+	struct file *file = vma->vm_file;
+	struct shm_file_data *sfd = shm_file_data(file);
+	struct shmid_kernel *shp;
+	int err = 0;
+
+	shp = shm_lock(sfd->ns, sfd->id);
+
+	if (IS_ERR(shp)) {
+		err = PTR_ERR(shp);
+		goto warn;
+	}
+
+	shp->shm_atim = ktime_get_real_seconds();
+	shp->shm_lprid = task_tgid_vnr(current);
+	shp->shm_nattch++;
+	shm_unlock(shp);
+
 	/*
 	 * We raced in the idr lookup or with shm_destroy().
 	 * Either way, the ID is busted.
 	 */
+warn:
 	WARN_ON_ONCE(err);
 }

@@ -418,19 +416,10 @@ static int shm_mmap(struct file *file, struct vm_area_struct *vma)
 	struct shm_file_data *sfd = shm_file_data(file);
 	int ret;

-	/*
-	 * In case of remap_file_pages() emulation, the file can represent
-	 * removed IPC ID: propogate shm_lock() error to caller.
-	 */
-	ret = __shm_open(vma);
-	if (ret)
-		return ret;
-
 	ret = call_mmap(sfd->file, vma);
-	if (ret) {
-		shm_close(vma);
+	if (ret)
 		return ret;
-	}
+
 	sfd->vm_ops = vma->vm_ops;
 #ifdef CONFIG_MMU
 	WARN_ON(!sfd->vm_ops->fault);
@@ -944,6 +933,7 @@ static int shmctl_stat(struct ipc_namespace *ns, int shmid,
 	tbuf->shm_cpid	= shp->shm_cprid;
 	tbuf->shm_lpid	= shp->shm_lprid;
 	tbuf->shm_nattch = shp->shm_nattch;
+
 	rcu_read_unlock();
 	return result;

@@ -1351,7 +1341,11 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,

 	path = shp->shm_file->f_path;
 	path_get(&path);
+
+	shp->shm_atim = ktime_get_real_seconds();
+	shp->shm_lprid = task_tgid_vnr(current);
 	shp->shm_nattch++;
+
 	size = i_size_read(d_inode(path.dentry));
 	ipc_unlock_object(&shp->shm_perm);
 	rcu_read_unlock();
@@ -1411,6 +1405,8 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,

 out_fput:
 	fput(file);
+	if (!err)
+		goto out;

 out_nattch:
 	down_write(&shm_ids(ns).rwsem);
--
2.11.0

  reply	other threads:[~2017-11-30  6:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-30  6:12 [PATCH 0/2] ipc: Fix <ipc>ctl(..IPC_STAT..) bugs Philippe Mikoyan
2017-11-30  6:12 ` Philippe Mikoyan [this message]
2017-11-30  6:12 ` [PATCH 2/2] ipc: Fix ipc data structures inconsistency Philippe Mikoyan
2017-12-01 17:20   ` Davidlohr Bueso
2017-12-02  6:03     ` Manfred Spraul
2017-12-02 14:43       ` Philippe Mikoyan
2017-12-03  1:37         ` Davidlohr Bueso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171130061224.25466-2-philippe.mikoyan@skat.systems \
    --to=philippe.mikoyan@skat.systems \
    --cc=akpm@linux-foundation.org \
    --cc=edgar.kaziakhmedov@virtuozzo.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=manfred@colorfullife.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®