Revision 73223e4e2e3867ebf033a5a8eb2e5df0158ccc99 authored by zhong jiang on 18 August 2017, 22:16:24 UTC, committed by Linus Torvalds on 18 August 2017, 22:32:02 UTC
I hit a use after free issue when executing trinity and repoduced it
with KASAN enabled.  The related call trace is as follows.

  BUG: KASan: use after free in SyS_get_mempolicy+0x3c8/0x960 at addr ffff8801f582d766
  Read of size 2 by task syz-executor1/798

  INFO: Allocated in mpol_new.part.2+0x74/0x160 age=3 cpu=1 pid=799
     __slab_alloc+0x768/0x970
     kmem_cache_alloc+0x2e7/0x450
     mpol_new.part.2+0x74/0x160
     mpol_new+0x66/0x80
     SyS_mbind+0x267/0x9f0
     system_call_fastpath+0x16/0x1b
  INFO: Freed in __mpol_put+0x2b/0x40 age=4 cpu=1 pid=799
     __slab_free+0x495/0x8e0
     kmem_cache_free+0x2f3/0x4c0
     __mpol_put+0x2b/0x40
     SyS_mbind+0x383/0x9f0
     system_call_fastpath+0x16/0x1b
  INFO: Slab 0xffffea0009cb8dc0 objects=23 used=8 fp=0xffff8801f582de40 flags=0x200000000004080
  INFO: Object 0xffff8801f582d760 @offset=5984 fp=0xffff8801f582d600

  Bytes b4 ffff8801f582d750: ae 01 ff ff 00 00 00 00 5a 5a 5a 5a 5a 5a 5a 5a  ........ZZZZZZZZ
  Object ffff8801f582d760: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b  kkkkkkkkkkkkkkkk
  Object ffff8801f582d770: 6b 6b 6b 6b 6b 6b 6b a5                          kkkkkkk.
  Redzone ffff8801f582d778: bb bb bb bb bb bb bb bb                          ........
  Padding ffff8801f582d8b8: 5a 5a 5a 5a 5a 5a 5a 5a                          ZZZZZZZZ
  Memory state around the buggy address:
  ffff8801f582d600: fb fb fb fc fc fc fc fc fc fc fc fc fc fc fc fc
  ffff8801f582d680: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
  >ffff8801f582d700: fc fc fc fc fc fc fc fc fc fc fc fc fb fb fb fc

!shared memory policy is not protected against parallel removal by other
thread which is normally protected by the mmap_sem.  do_get_mempolicy,
however, drops the lock midway while we can still access it later.

Early premature up_read is a historical artifact from times when
put_user was called in this path see https://lwn.net/Articles/124754/
but that is gone since 8bccd85ffbaf ("[PATCH] Implement sys_* do_*
layering in the memory policy layer.").  but when we have the the
current mempolicy ref count model.  The issue was introduced
accordingly.

Fix the issue by removing the premature release.

Link: http://lkml.kernel.org/r/1502950924-27521-1-git-send-email-zhongjiang@huawei.com
Signed-off-by: zhong jiang <zhongjiang@huawei.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: David Rientjes <rientjes@google.com>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: <stable@vger.kernel.org>	[2.6+]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent da094e4
Raw File
mpoa_caches.h
#ifndef MPOA_CACHES_H
#define MPOA_CACHES_H

#include <linux/netdevice.h>
#include <linux/types.h>
#include <linux/atm.h>
#include <linux/atmdev.h>
#include <linux/atmmpc.h>
#include <linux/refcount.h>

struct mpoa_client;

void atm_mpoa_init_cache(struct mpoa_client *mpc);

typedef struct in_cache_entry {
	struct in_cache_entry *next;
	struct in_cache_entry *prev;
	struct timeval  tv;
	struct timeval  reply_wait;
	struct timeval  hold_down;
	uint32_t  packets_fwded;
	uint16_t  entry_state;
	uint32_t retry_time;
	uint32_t refresh_time;
	uint32_t count;
	struct   atm_vcc *shortcut;
	uint8_t  MPS_ctrl_ATM_addr[ATM_ESA_LEN];
	struct   in_ctrl_info ctrl_info;
	refcount_t use;
} in_cache_entry;

struct in_cache_ops{
	in_cache_entry *(*add_entry)(__be32 dst_ip,
				      struct mpoa_client *client);
	in_cache_entry *(*get)(__be32 dst_ip, struct mpoa_client *client);
	in_cache_entry *(*get_with_mask)(__be32 dst_ip,
					 struct mpoa_client *client,
					 __be32 mask);
	in_cache_entry *(*get_by_vcc)(struct atm_vcc *vcc,
				      struct mpoa_client *client);
	void            (*put)(in_cache_entry *entry);
	void            (*remove_entry)(in_cache_entry *delEntry,
					struct mpoa_client *client );
	int             (*cache_hit)(in_cache_entry *entry,
				     struct mpoa_client *client);
	void            (*clear_count)(struct mpoa_client *client);
	void            (*check_resolving)(struct mpoa_client *client);
	void            (*refresh)(struct mpoa_client *client);
	void            (*destroy_cache)(struct mpoa_client *mpc);
};

typedef struct eg_cache_entry{
	struct               eg_cache_entry *next;
	struct               eg_cache_entry *prev;
	struct               timeval  tv;
	uint8_t              MPS_ctrl_ATM_addr[ATM_ESA_LEN];
	struct atm_vcc       *shortcut;
	uint32_t             packets_rcvd;
	uint16_t             entry_state;
	__be32             latest_ip_addr;    /* The src IP address of the last packet */
	struct eg_ctrl_info  ctrl_info;
	refcount_t             use;
} eg_cache_entry;

struct eg_cache_ops{
	eg_cache_entry *(*add_entry)(struct k_message *msg, struct mpoa_client *client);
	eg_cache_entry *(*get_by_cache_id)(__be32 cache_id, struct mpoa_client *client);
	eg_cache_entry *(*get_by_tag)(__be32 cache_id, struct mpoa_client *client);
	eg_cache_entry *(*get_by_vcc)(struct atm_vcc *vcc, struct mpoa_client *client);
	eg_cache_entry *(*get_by_src_ip)(__be32 ipaddr, struct mpoa_client *client);
	void            (*put)(eg_cache_entry *entry);
	void            (*remove_entry)(eg_cache_entry *entry, struct mpoa_client *client);
	void            (*update)(eg_cache_entry *entry, uint16_t holding_time);
	void            (*clear_expired)(struct mpoa_client *client);
	void            (*destroy_cache)(struct mpoa_client *mpc);
};


/* Ingress cache entry states */

#define INGRESS_REFRESHING 3
#define INGRESS_RESOLVED   2
#define INGRESS_RESOLVING  1
#define INGRESS_INVALID    0

/* VCC states */

#define OPEN   1
#define CLOSED 0

/* Egress cache entry states */

#define EGRESS_RESOLVED 2
#define EGRESS_PURGE    1
#define EGRESS_INVALID  0

#endif
back to top