From owner-freebsd-hackers@FreeBSD.ORG Sun Jan 20 23:02:05 2013 Return-Path: Delivered-To: hackers@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 8B6124E6 for ; Sun, 20 Jan 2013 23:02:05 +0000 (UTC) (envelope-from yuri@rawbw.com) Received: from shell0.rawbw.com (shell0.rawbw.com [198.144.192.45]) by mx1.freebsd.org (Postfix) with ESMTP id 706D2A13 for ; Sun, 20 Jan 2013 23:02:05 +0000 (UTC) Received: from eagle.yuri.org (stunnel@localhost [127.0.0.1]) (authenticated bits=0) by shell0.rawbw.com (8.14.4/8.14.4) with ESMTP id r0KN1xWP034885 for ; Sun, 20 Jan 2013 15:01:59 -0800 (PST) (envelope-from yuri@rawbw.com) Message-ID: <50FC7767.4050207@rawbw.com> Date: Sun, 20 Jan 2013 15:01:59 -0800 From: Yuri User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:17.0) Gecko/20130112 Thunderbird/17.0.2 MIME-Version: 1.0 To: hackers@freebsd.org Subject: How to validate the variable size memory block in ioctl handler? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 20 Jan 2013 23:02:05 -0000 I am implementing an ioctl that reads/writes variable size structure. Allocated size is supplied by the caller in the structure itself. struct my_struct { int len; // allocated size other_struct s[1]; }; ioctl request id is defined as _IOWR('X', , my_struct) How to validate from the ioctl function handler (for some device) that the whole (variable size) block of bytes is RW accessible in the process memory space? Should I call copyout/copyin for this, or there is some shorter way? EFAULT should be returned in case of validation failure. As I understand, macros like _IOR, _IOWR do validation based on the size of structure supplied to them. So that the handler procedures don't have to do that. I was expecting to find among them some macro that would work for such variable size structure, but it isn't there. (Not sure if this is possible language-wise). Yuri