Annotation of truecrypt/linux/kernel/dm-target.c, revision 1.1.1.5

1.1.1.5 ! root        1: /*
        !             2:  Copyright (c) TrueCrypt Foundation. All rights reserved.
1.1       root        3: 
1.1.1.5 ! root        4:  Covered by the TrueCrypt License 2.2 the full text of which is contained
        !             5:  in the file License.txt included in TrueCrypt binary and source code
        !             6:  distribution packages.
1.1       root        7: */
                      8: 
                      9: #include <linux/bio.h>
1.1.1.4   root       10: #include <linux/blkdev.h>
1.1       root       11: #include <linux/ctype.h>
1.1.1.4   root       12: #include <linux/device-mapper.h>
1.1       root       13: #include <linux/init.h>
1.1.1.4   root       14: #include <linux/kdev_t.h>
1.1       root       15: #include <linux/module.h>
                     16: #include <linux/moduleparam.h>
                     17: #include <linux/version.h>
                     18: #include <linux/workqueue.h>
1.1.1.4   root       19: #include <dm.h>
1.1       root       20: 
                     21: #include "Tcdefs.h"
                     22: #include "Crypto.h"
                     23: #include "Tests.h"
                     24: #include "Dm-target.h"
                     25: 
                     26: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
                     27: #error Linux kernel 2.6.5 or later required
                     28: #endif
                     29: 
                     30: int trace_level = 0;
                     31: 
                     32: #define MSG_PREFIX "truecrypt: "
                     33: #define error(fmt, args...) printk(KERN_ERR MSG_PREFIX fmt, ## args)
                     34: #define trace(level, fmt, args...) level <= trace_level && printk(KERN_DEBUG MSG_PREFIX fmt, ## args)
                     35: #define dbg(fmt, args...) printk(KERN_DEBUG MSG_PREFIX fmt, ## args)
                     36: 
                     37: #define MIN_POOL_SIZE 16
                     38: 
1.1.1.4   root       39: #ifndef __GFP_NOMEMALLOC
                     40: #define __GFP_NOMEMALLOC 0
                     41: #endif
                     42: 
1.1       root       43: struct target_ctx
                     44: {
                     45:        struct dm_dev *dev;
                     46:        sector_t start;
                     47:        char *volume_path;
                     48:        mempool_t *bio_ctx_pool;
                     49:        mempool_t *pg_pool;
1.1.1.4   root       50:        unsigned long long read_only_start;
                     51:        unsigned long long read_only_end;
1.1.1.5 ! root       52:        unsigned long long uid;
1.1.1.4   root       53:        unsigned long long mtime;
                     54:        unsigned long long atime;
1.1       root       55:        int flags;
1.1.1.2   root       56:        PCRYPTO_INFO ci;
1.1       root       57: };
                     58: 
                     59: struct bio_ctx
                     60: {
                     61:        struct dm_target *target;
                     62:        struct bio *orig_bio;
                     63:        atomic_t ref_count;
                     64:        u64 crypto_sector;
                     65:        int error;
                     66:        struct work_struct work;
                     67: };
                     68: 
                     69: static struct workqueue_struct *work_queue = NULL;
1.1.1.5 ! root       70: 
        !            71: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
        !            72: static struct kmem_cache *bio_ctx_cache = NULL;
        !            73: #else
1.1       root       74: static kmem_cache_t *bio_ctx_cache = NULL;
1.1.1.5 ! root       75: #endif
1.1       root       76: 
1.1.1.4   root       77: #define READ_ONLY(tc) (tc->flags & TC_READ_ONLY)
                     78: #define HID_VOL_PROT(tc) (tc->flags & TC_HIDDEN_VOLUME_PROTECTION)
1.1       root       79: 
                     80: 
                     81: static int hex2bin (char *hex_string, u8 *byte_buf, int max_length)
                     82: {
                     83:        int i = 0, n;
                     84:        char s[3];
                     85:        s[2] = 0;
                     86: 
                     87:        while (i < max_length
                     88:                && (s[0] = *hex_string++) 
                     89:                && (s[1] = *hex_string++))
                     90:        {
                     91:                if (sscanf (s, "%x", &n) != 1)
                     92:                        return 0;
                     93:                byte_buf[i++] = (u8) n;
                     94:        }
                     95: 
                     96:        return i;
                     97: }
                     98: 
1.1.1.5 ! root       99: 
        !           100: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
        !           101: #define congestion_wait blk_congestion_wait
        !           102: #endif
        !           103: 
1.1.1.4   root      104: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
                    105: 
                    106: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
                    107: static void *mempool_alloc_pg (gfp_t gfp_mask, void *pool_data)
                    108: #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1.1       root      109: static void *mempool_alloc_pg (unsigned int gfp_mask, void *pool_data)
                    110: #else
                    111: static void *mempool_alloc_pg (int gfp_mask, void *pool_data)
                    112: #endif
                    113: {
                    114:        return alloc_page (gfp_mask);
                    115: }
                    116: 
                    117: static void mempool_free_pg (void *element, void *pool_data)
                    118: {
                    119:        __free_page (element);
                    120: }
                    121: 
1.1.1.4   root      122: #endif // LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
                    123: 
                    124: 
                    125: static void *malloc_wait (mempool_t *pool, int direction)
                    126: {
                    127:        void *p;
                    128:        while (1)
                    129:        {
                    130:                p = mempool_alloc (pool, GFP_NOIO | __GFP_NOMEMALLOC);
                    131: 
                    132:                if (p)
                    133:                        return p;
                    134: 
1.1.1.5 ! root      135:                congestion_wait (direction, HZ / 50);
1.1.1.4   root      136:        }
                    137: }
                    138: 
1.1       root      139: 
1.1.1.2   root      140: static void wipe_args (unsigned int argc, char **argv)
                    141: {
                    142:        int i;
                    143:        for (i = 0; i < argc; i++)
                    144:        {
                    145:                if (argv[i] != NULL)
                    146:                        burn (argv[i], strlen (argv[i]));
                    147:        }
                    148: }
                    149: 
                    150: 
1.1       root      151: static int truecrypt_ctr (struct dm_target *ti, unsigned int argc, char **argv)
                    152: {
                    153:        struct target_ctx *tc;
                    154:        int key_size;
                    155:        int error = -EINVAL;
1.1.1.4   root      156:        unsigned long long sector;
1.1       root      157: 
1.1.1.4   root      158:        if (argc != TC_LAST_ARG + 1)
1.1       root      159:        {
1.1.1.2   root      160:                ti->error = "truecrypt: Usage: <start_sector> <sector_count> truecrypt <EA> <mode> <key> <key2/IV> <host_device> <sector_offset> <read_only_start> <read_only_end> <mtime> <atime> <flags> <volume_path>";
1.1       root      161:                return -EINVAL;
                    162:        }
                    163: 
                    164:        tc = kmalloc (sizeof (*tc), GFP_KERNEL);
                    165:        if (tc == NULL)
                    166:        {
                    167:                ti->error = "truecrypt: Cannot allocate target context";
                    168:                error = -ENOMEM;
                    169:                goto err;
                    170:        }
                    171:        memset (tc, 0, sizeof (*tc));
                    172: 
1.1.1.2   root      173:        tc->ci = crypto_open ();
                    174:        if (tc == NULL)
                    175:        {
                    176:                ti->error = "truecrypt: Cannot allocate crypto_info";
                    177:                error = -ENOMEM;
                    178:                goto err;
                    179:        }
                    180: 
1.1.1.4   root      181: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
                    182:        tc->bio_ctx_pool = mempool_create_slab_pool (MIN_POOL_SIZE, bio_ctx_cache);
                    183: #else
1.1       root      184:        tc->bio_ctx_pool = mempool_create (MIN_POOL_SIZE, mempool_alloc_slab, mempool_free_slab, bio_ctx_cache);
1.1.1.4   root      185: #endif
                    186: 
1.1       root      187:        if (!tc->bio_ctx_pool)
                    188:        {
                    189:                ti->error = "truecrypt: Cannot create bio context memory pool";
                    190:                error = -ENOMEM;
                    191:                goto err;
                    192:        }
                    193: 
1.1.1.4   root      194: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
                    195:        tc->pg_pool = mempool_create_page_pool (MIN_POOL_SIZE, 0);
                    196: #else
1.1       root      197:        tc->pg_pool = mempool_create (MIN_POOL_SIZE, mempool_alloc_pg, mempool_free_pg, NULL);
1.1.1.4   root      198: #endif
1.1       root      199:        if (!tc->pg_pool)
                    200:        {
                    201:                ti->error = "truecrypt: Cannot create page memory pool";
                    202:                error = -ENOMEM;
                    203:                goto err;
                    204:        }
                    205: 
1.1.1.4   root      206:        if (sscanf (argv[TC_ARG_SEC], "%llu", &sector) != 1)
1.1       root      207:        {
                    208:                ti->error = "truecrypt: Invalid device sector";
                    209:                goto err;
                    210:        }
1.1.1.4   root      211:        tc->start = sector;
1.1       root      212: 
1.1.1.4   root      213:        if (dm_get_device (ti, argv[TC_ARG_DEV], tc->start, ti->len, dm_table_get_mode (ti->table), &tc->dev))
1.1       root      214:        {
                    215:                ti->error = "truecrypt: Device lookup failed";
                    216:                goto err;
                    217:        }
                    218: 
                    219:        // Encryption algorithm
1.1.1.2   root      220:        tc->ci->ea = 0;
1.1.1.4   root      221:        if (sscanf (argv[TC_ARG_EA], "%d", &tc->ci->ea) != 1
1.1.1.2   root      222:                || tc->ci->ea < EAGetFirst ()
                    223:                || tc->ci->ea > EAGetCount ())
1.1       root      224:        {
                    225:                ti->error = "truecrypt: Invalid encryption algorithm";
                    226:                goto err;
                    227:        }
                    228: 
1.1.1.2   root      229:        // Mode of operation
                    230:        tc->ci->mode = 0;
1.1.1.4   root      231:        if (sscanf (argv[TC_ARG_MODE], "%d", &tc->ci->mode) != 1
1.1.1.2   root      232:                || tc->ci->mode < 1
                    233:                || tc->ci->mode >= INVALID_MODE)
1.1       root      234:        {
1.1.1.2   root      235:                ti->error = "truecrypt: Invalid mode of operation";
1.1       root      236:                goto err;
                    237:        }
                    238: 
1.1.1.2   root      239:        // Key
                    240:        key_size = EAGetKeySize (tc->ci->ea);
1.1.1.4   root      241:        if (hex2bin (argv[TC_ARG_KEY], tc->ci->master_key, key_size) != key_size)
1.1       root      242:        {
                    243:                ti->error = "truecrypt: Invalid key";
                    244:                goto err;
                    245:        }
1.1.1.2   root      246:        
                    247:        // EA init
                    248:        if (EAInit (tc->ci->ea, tc->ci->master_key, tc->ci->ks) == ERR_CIPHER_INIT_FAILURE)
1.1       root      249:        {
1.1.1.2   root      250:                ti->error = "truecrypt: Encryption algorithm initialization failed";
1.1       root      251:                goto err;
                    252:        }
                    253: 
1.1.1.2   root      254:        // Key2 / IV
1.1.1.4   root      255:        if (hex2bin (argv[TC_ARG_IV], tc->ci->iv, sizeof (tc->ci->iv)) != sizeof (tc->ci->iv))
1.1       root      256:        {
                    257:                ti->error = "truecrypt: Invalid IV";
                    258:                goto err;
                    259:        }
                    260: 
1.1.1.2   root      261:        // Mode init    
                    262:        if (!EAInitMode (tc->ci))
                    263:        {
                    264:                ti->error = "truecrypt: Mode of operation initialization failed";
                    265:                goto err;
                    266:        }
                    267: 
1.1       root      268:        // Read-only start sector
1.1.1.4   root      269:        if (sscanf (argv[TC_ARG_RO_START], "%llu", &tc->read_only_start) != 1)
1.1       root      270:        {
                    271:                ti->error = "truecrypt: Invalid read-only start sector";
                    272:                goto err;
                    273:        }
                    274: 
                    275:        // Read-only end sector
1.1.1.4   root      276:        if (sscanf (argv[TC_ARG_RO_END], "%llu", &tc->read_only_end) != 1)
1.1       root      277:        {
                    278:                ti->error = "truecrypt: Invalid read-only end sector";
                    279:                goto err;
                    280:        }
                    281: 
1.1.1.5 ! root      282:        // User ID
        !           283:        if (sscanf (argv[TC_ARG_UID], "%llu", &tc->uid) != 1)
        !           284:        {
        !           285:                ti->error = "truecrypt: Invalid user ID";
        !           286:                goto err;
        !           287:        }
        !           288: 
1.1       root      289:        // Modification time
1.1.1.4   root      290:        if (sscanf (argv[TC_ARG_MTIME], "%llu", &tc->mtime) != 1)
1.1       root      291:        {
                    292:                ti->error = "truecrypt: Invalid modification time";
                    293:                goto err;
                    294:        }
                    295: 
                    296:        // Access time
1.1.1.4   root      297:        if (sscanf (argv[TC_ARG_ATIME], "%llu", &tc->atime) != 1)
1.1       root      298:        {
                    299:                ti->error = "truecrypt: Invalid access time";
                    300:                goto err;
                    301:        }
                    302: 
                    303:        // Flags
1.1.1.4   root      304:        if (sscanf (argv[TC_ARG_FLAGS], "%d", &tc->flags) != 1)
1.1       root      305:        {
                    306:                ti->error = "truecrypt: Invalid flags";
                    307:                goto err;
                    308:        }
                    309: 
                    310:        // Volume path
1.1.1.4   root      311:        tc->volume_path = kmalloc (strlen (argv[TC_ARG_VOL]) + 1, GFP_KERNEL);
1.1       root      312:        if (tc->volume_path == NULL)
                    313:        {
                    314:                ti->error = "truecrypt: Cannot allocate volume path buffer";
                    315:                error = -ENOMEM;
                    316:                goto err;
                    317:        }
1.1.1.4   root      318:        strcpy (tc->volume_path, argv[TC_ARG_VOL]);
1.1       root      319: 
1.1.1.2   root      320:        // Hidden volume
                    321:        if (tc->start > 1)
                    322:        {
                    323:                tc->ci->hiddenVolume = TRUE;
                    324:                tc->ci->hiddenVolumeOffset = tc->start * SECTOR_SIZE;
                    325:        }
                    326: 
1.1       root      327:        ti->private = tc;
1.1.1.2   root      328: 
                    329:        wipe_args (argc, argv);
1.1       root      330:        return 0;
                    331: 
                    332: err:
                    333:        if (tc)
                    334:        {
1.1.1.2   root      335:                if (tc->ci)
                    336:                        crypto_close (tc->ci);
1.1       root      337:                if (tc->volume_path)
                    338:                        kfree (tc->volume_path);
                    339:                if (tc->bio_ctx_pool)
                    340:                        mempool_destroy (tc->bio_ctx_pool);
                    341:                if (tc->pg_pool)
                    342:                        mempool_destroy (tc->pg_pool);
                    343:                kfree (tc);
                    344:        }
                    345: 
1.1.1.2   root      346:        wipe_args (argc, argv);
1.1       root      347:        return error;
                    348: }
                    349: 
                    350: 
                    351: static void truecrypt_dtr (struct dm_target *ti)
                    352: {
                    353:        struct target_ctx *tc = (struct target_ctx *) ti->private;
                    354: 
                    355:        mempool_destroy (tc->bio_ctx_pool);
1.1.1.2   root      356:        mempool_destroy (tc->pg_pool);
                    357:        crypto_close (tc->ci);
1.1       root      358:        kfree(tc->volume_path);
1.1.1.2   root      359:        dm_put_device(ti, tc->dev);
1.1       root      360:        kfree(tc);
                    361: }
                    362: 
                    363: 
                    364: // Checks if two regions overlap (borders are parts of regions)
1.1.1.4   root      365: static int RegionsOverlap (u64 start1, u64 end1, u64 start2, u64 end2)
1.1       root      366: {
                    367:        return (start1 < start2) ? (end1 >= start2) : (start1 <= end2);
                    368: }
                    369: 
                    370: 
                    371: static void dereference_bio_ctx (struct bio_ctx *bc)
                    372: {
                    373:        struct target_ctx *tc = (struct target_ctx *) bc->target->private;
                    374: 
                    375:        if (!atomic_dec_and_test (&bc->ref_count))
                    376:                return;
                    377: 
                    378:        bio_endio (bc->orig_bio, bc->orig_bio->bi_size, bc->error);
                    379:        mempool_free (bc, tc->bio_ctx_pool);
                    380: }
                    381: 
                    382: 
1.1.1.5 ! root      383: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
        !           384: static void work_process (struct work_struct *qdata)
        !           385: {
        !           386:        struct bio_ctx *bc = container_of(qdata, struct bio_ctx, work);
        !           387: #else
1.1.1.4   root      388: static void work_process (void *qdata)
1.1       root      389: {
1.1.1.4   root      390:        struct bio_ctx *bc = (struct bio_ctx *) qdata;
1.1.1.5 ! root      391: #endif
        !           392: 
1.1       root      393:        struct target_ctx *tc = (struct target_ctx *) bc->target->private;
                    394:        struct bio_vec *bv;
1.1.1.4   root      395:        u64 sec_no = bc->crypto_sector;
1.1       root      396:        int seg_no;
                    397:        unsigned long flags;
                    398: 
                    399:        // Decrypt queued data
                    400:        bio_for_each_segment (bv, bc->orig_bio, seg_no)
                    401:        {
                    402:                unsigned int secs = bv->bv_len / SECTOR_SIZE;
                    403:                char *data = bvec_kmap_irq (bv, &flags);
                    404: 
1.1.1.2   root      405:                DecryptSectors ((unsigned __int32 *)data, sec_no, secs, tc->ci);
1.1       root      406: 
                    407:                sec_no += secs;
                    408: 
                    409:                flush_dcache_page (bv->bv_page);
                    410:                bvec_kunmap_irq (data, &flags);
1.1.1.4   root      411: 
                    412:                if (seg_no + 1 < bc->orig_bio->bi_vcnt)
                    413:                        cond_resched ();
1.1       root      414:        }
                    415: 
                    416:        dereference_bio_ctx (bc);
                    417: }
                    418: 
                    419: 
                    420: static int truecrypt_endio (struct bio *bio, unsigned int bytes_done, int error)
                    421: {
                    422:        struct bio_ctx *bc = (struct bio_ctx *) bio->bi_private;
                    423:        struct target_ctx *tc = (struct target_ctx *) bc->target->private;
                    424:        struct bio_vec *bv;
                    425:        int seg_no;
                    426:        
1.1.1.4   root      427:        trace (1, "end: sc=%llu fl=%ld rw=%ld sz=%d ix=%hd vc=%hd dn=%d er=%d\n",
                    428:                (unsigned long long) bio->bi_sector, bio->bi_flags, bio->bi_rw, bio->bi_size, bio->bi_idx, bio->bi_vcnt, bytes_done, error);
1.1       root      429: 
                    430:        if (error != 0)
                    431:                bc->error = error;
                    432: 
                    433:        if (bio->bi_size)
                    434:        {
                    435:                trace (2, "Outstanding IO: %d\n", bio->bi_size);
                    436:                return 1;
                    437:        }
                    438: 
                    439:        if (bio_data_dir (bio) == READ)
                    440:        {
                    441:                bio_put (bio);
                    442: 
                    443:                // Queue decryption to leave completion interrupt ASAP
1.1.1.5 ! root      444: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
        !           445:                INIT_WORK (&bc->work, work_process);
        !           446: #else
1.1       root      447:                INIT_WORK (&bc->work, work_process, bc);
1.1.1.5 ! root      448: #endif
1.1       root      449:                queue_work (work_queue, &bc->work);
                    450:                return error;
                    451:        }
                    452: 
                    453:        // Free pages allocated for encryption
                    454:        bio_for_each_segment (bv, bio, seg_no)
                    455:        {
                    456:                mempool_free (bv->bv_page, tc->pg_pool);  
                    457:        }
                    458: 
                    459:        bio_put (bio);
                    460:        dereference_bio_ctx (bc);
                    461:        return error;
                    462: }
                    463: 
                    464: 
                    465: static int truecrypt_map (struct dm_target *ti, struct bio *bio, union map_info *map_context)
                    466: {
                    467:        struct target_ctx *tc = (struct target_ctx *) ti->private;
                    468:        struct bio_ctx *bc;
                    469:        struct bio *bion;
                    470:        struct bio_vec *bv;
                    471:        int seg_no;
                    472: 
1.1.1.4   root      473:        trace (1, "map: sc=%llu fl=%ld rw=%ld sz=%d ix=%hd vc=%hd\n",
                    474:                (unsigned long long) bio->bi_sector, bio->bi_flags, bio->bi_rw, bio->bi_size, bio->bi_idx, bio->bi_vcnt);
1.1       root      475: 
                    476:        // Write protection
                    477:        if (bio_data_dir (bio) == WRITE && READ_ONLY (tc))
                    478:                return -EPERM;
                    479: 
                    480:        // Validate segment sizes
                    481:        bio_for_each_segment (bv, bio, seg_no)
                    482:        {
                    483:                if (bv->bv_len & (SECTOR_SIZE - 1))
                    484:                {
1.1.1.4   root      485:                        error ("unsupported bio segment size %d (%ld %d %hd %hd)\n",
1.1       root      486:                                bv->bv_len, bio->bi_rw, bio->bi_size, bio->bi_idx, bio->bi_vcnt);
                    487:                        return -EINVAL;
                    488:                }
                    489:        }
                    490: 
                    491:        // Bio context
1.1.1.4   root      492:        bc = malloc_wait (tc->bio_ctx_pool, bio_data_dir (bio));
1.1       root      493:        if (!bc)
                    494:        {
                    495:                error ("bio context allocation failed\n");
                    496:                return -ENOMEM;
                    497:        }
                    498: 
                    499:        atomic_set (&bc->ref_count, 1);
                    500:        bc->orig_bio = bio;
                    501:        bc->error = 0;
                    502:        bc->target = ti;
                    503:        bc->crypto_sector = tc->start + (bio->bi_sector - ti->begin);
                    504: 
                    505:        // New bio for encrypted device
1.1.1.4   root      506:        while (!(bion = bio_alloc (GFP_NOIO | __GFP_NOMEMALLOC, bio_segments (bio))))
1.1       root      507:        {
1.1.1.5 ! root      508:                congestion_wait (bio_data_dir (bio), HZ / 50);
1.1       root      509:        }
                    510: 
                    511:        bion->bi_bdev = tc->dev->bdev;
                    512:        bion->bi_end_io = truecrypt_endio;
                    513:        bion->bi_idx = 0;
                    514:        bion->bi_private = bc;
                    515:        bion->bi_rw = bio->bi_rw;
                    516:        bion->bi_sector = bc->crypto_sector;
                    517:        bion->bi_size = bio->bi_size;
                    518:        bion->bi_vcnt = bio_segments (bio);
                    519: 
                    520:        if (bio_data_dir (bio) == READ)
                    521:        {
                    522:                // Buffers of originating bio can be used for decryption
                    523:                memcpy (bion->bi_io_vec,
                    524:                        bio_iovec (bio),
                    525:                        bion->bi_vcnt * sizeof (struct bio_vec));
                    526:        }
                    527:        else
                    528:        {
1.1.1.4   root      529:                u64 sec_no = bc->crypto_sector;
                    530:                int seg_no;
                    531: 
1.1       root      532:                // Encrypt data to be written
                    533:                unsigned long flags, copyFlags;
                    534:                char *data, *copy;
                    535: 
                    536:                memset (bion->bi_io_vec, 0, sizeof (struct bio_vec) * bion->bi_vcnt);
                    537: 
                    538:                bio_for_each_segment (bv, bio, seg_no)
                    539:                {
                    540:                        struct bio_vec *cbv = bio_iovec_idx (bion, seg_no);
                    541:                        unsigned int secs = bv->bv_len / SECTOR_SIZE;
                    542: 
                    543:                        // Hidden volume protection
                    544:                        if (!READ_ONLY (tc) && HID_VOL_PROT (tc)
                    545:                                && RegionsOverlap (sec_no, sec_no + secs - 1, tc->read_only_start, tc->read_only_end))
                    546:                        {
1.1.1.4   root      547:                                tc->flags |= TC_READ_ONLY | TC_PROTECTION_ACTIVATED;
1.1       root      548:                        }
                    549: 
1.1.1.4   root      550:                        if (READ_ONLY (tc))
1.1       root      551:                        {
1.1.1.4   root      552:                                // Write not permitted
1.1       root      553:                                bio_for_each_segment (cbv, bion, seg_no)
                    554:                                {
                    555:                                        if (cbv->bv_page != NULL)
                    556:                                                mempool_free (cbv->bv_page, tc->pg_pool);  
                    557:                                }
                    558: 
                    559:                                bio_put (bion);
                    560:                                bc->error = READ_ONLY (tc) ? -EPERM : -ENOMEM;
                    561:                                dereference_bio_ctx (bc);
                    562:                                return 0;
                    563:                        }
1.1.1.4   root      564: 
                    565:                        cbv->bv_page = malloc_wait (tc->pg_pool, bio_data_dir (bion));
1.1       root      566: 
                    567:                        cbv->bv_offset = 0;
                    568:                        cbv->bv_len = bv->bv_len;
                    569: 
                    570:                        copy = bvec_kmap_irq (cbv, &copyFlags);
1.1.1.3   root      571:                        data = bvec_kmap_irq (bv, &flags);
1.1       root      572: 
                    573:                        memcpy (copy, data, bv->bv_len);
                    574: 
1.1.1.4   root      575:                        EncryptSectors ((unsigned __int32 *) copy, sec_no, secs, tc->ci);
1.1       root      576:                        sec_no += secs;
                    577: 
1.1.1.4   root      578:                        bvec_kunmap_irq (data, &flags);
1.1       root      579:                        bvec_kunmap_irq (copy, &copyFlags);
1.1.1.4   root      580:                        flush_dcache_page (bv->bv_page);
                    581:                        flush_dcache_page (cbv->bv_page);
                    582: 
                    583:                        if (seg_no + 1 < bio->bi_vcnt)
                    584:                                cond_resched();
1.1       root      585:                }
                    586:        }
                    587: 
                    588:        atomic_inc (&bc->ref_count);
                    589:        generic_make_request (bion);
                    590: 
                    591:        dereference_bio_ctx (bc);
                    592:        return 0;
                    593: }
                    594: 
                    595: 
                    596: static int truecrypt_status (struct dm_target *ti, status_type_t type, char *result, unsigned int maxlen)
                    597: {
                    598:        struct target_ctx *tc = (struct target_ctx *) ti->private;
                    599: 
                    600:        switch (type)
                    601:        {
                    602:        case STATUSTYPE_INFO:
                    603:                result[0] = 0;
                    604:                break;
                    605: 
                    606:        case STATUSTYPE_TABLE:
                    607:                {
                    608:                        char name[32];
                    609:                        format_dev_t (name, tc->dev->bdev->bd_dev);
1.1.1.5 ! root      610:                        snprintf (result, maxlen, "%d %d 0 0 %s %llu %llu %llu %llu %llu %llu %d %s",
1.1.1.2   root      611:                                tc->ci->ea,
                    612:                                tc->ci->mode,
1.1       root      613:                                name,
1.1.1.4   root      614:                                (unsigned long long) tc->start,
1.1       root      615:                                tc->read_only_start,
                    616:                                tc->read_only_end,
1.1.1.5 ! root      617:                                tc->uid,
1.1       root      618:                                tc->mtime,
                    619:                                tc->atime,
                    620:                                tc->flags,
                    621:                                tc->volume_path);
                    622:                }
                    623:                break;
                    624:        }
                    625: 
                    626:        return 0;
                    627: }
                    628: 
                    629: 
                    630: static struct target_type truecrypt_target = {
                    631:        .name   = "truecrypt",
1.1.1.4   root      632:        .version= {TC_VERSION_NUM1, TC_VERSION_NUM2, TC_VERSION_NUM3},
1.1       root      633:        .module = THIS_MODULE,
                    634:        .ctr    = truecrypt_ctr,
                    635:        .dtr    = truecrypt_dtr,
                    636:        .map    = truecrypt_map,
                    637:        .status = truecrypt_status
                    638: };
                    639: 
                    640: 
                    641: int __init dm_truecrypt_init(void)
                    642: {
                    643:        int r;
                    644: 
                    645:        if (!AutoTestAlgorithms ())
                    646:        {
1.1.1.5 ! root      647:                error ("self-test of algorithms failed");
1.1       root      648:                return -ERANGE;
                    649:        }
                    650: 
                    651:        work_queue = create_workqueue ("truecryptq");
                    652: 
                    653:        if (!work_queue)
                    654:        {
1.1.1.5 ! root      655:                error ("create_workqueue failed");
1.1       root      656:                goto err;
                    657:        }
                    658: 
                    659:        bio_ctx_cache = kmem_cache_create ("truecrypt-bioctx", sizeof (struct bio_ctx), 0, 0, NULL, NULL);
                    660:        if (!bio_ctx_cache)
                    661:        {
1.1.1.5 ! root      662:                error ("kmem_cache_create failed");
1.1       root      663:                goto err;
                    664:        }
                    665: 
                    666:        r = dm_register_target (&truecrypt_target);
                    667:        if (r < 0)
                    668:        {
1.1.1.5 ! root      669:                error ("register failed %d", r);
1.1       root      670:                goto err;
                    671:        }
                    672: 
                    673:        return r;
                    674: 
                    675: err:
                    676:        if (work_queue)
                    677:                destroy_workqueue (work_queue);
                    678:        if (bio_ctx_cache)
                    679:                kmem_cache_destroy (bio_ctx_cache);
                    680: 
                    681:        return -ENOMEM;
                    682: }
                    683: 
                    684: 
                    685: void __exit dm_truecrypt_exit(void)
                    686: {
                    687:        int r;
                    688: 
                    689:        r = dm_unregister_target (&truecrypt_target);
                    690: 
                    691:        if (r < 0)
1.1.1.5 ! root      692:                error ("unregister failed %d", r);
1.1       root      693: 
                    694:        destroy_workqueue (work_queue);
                    695:        kmem_cache_destroy (bio_ctx_cache);
                    696: }
                    697: 
                    698: 
                    699: module_init(dm_truecrypt_init);
                    700: module_exit(dm_truecrypt_exit);
                    701: module_param_named(trace, trace_level, int, 0);
                    702: 
                    703: MODULE_AUTHOR("TrueCrypt Foundation");
1.1.1.5 ! root      704: MODULE_DESCRIPTION("device-mapper target for encryption and decryption of TrueCrypt volumes");
1.1       root      705: MODULE_PARM_DESC(trace, "Trace level");
1.1.1.5 ! root      706: MODULE_LICENSE("GPL and additional rights");

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.